Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Count Vowels in Substrings

Prefix-sum a 0/1 indicator

Given a string, precompute prefix counts of vowels so that the number of vowels in any substring range can be answered in constant time per query.

step 1 / 18
l
e
e
t
c
o
d
e
[0][1][2][3][4][5][6][7]
Brute force · scan each query
1given s, queries
2for each query (l, r):
3 count = 0
4 for k in l..r: if s[k] is vowel: count++
5 answer the query with count
state
  • s"leetcode"
  • queries2

line 1Answer queries of the form "how many VOWELS are in s[l..r]?" (vowels are shown in gold). The direct way: for each query, walk the range and count.