l
e
e
t
c
o
d
e
[0][1][2][3][4][5][6][7]
Brute force · scan each query
▸1given s, queries2for each query (l, r):3 count = 04 for k in l..r: if s[k] is vowel: count++5 answer the query with count
state
- s"leetcode"
- queries2
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 problemGiven 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.
▸1given s, queries2for each query (l, r):3 count = 04 for k in l..r: if s[k] is vowel: count++5 answer the query with count
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.