Count Vowel Strings in Ranges
01 · Problem
You are given a 0-indexed array of strings words and a 2D array queries, where each queries[i] = [l, r].
Call a word a vowel string if its first character and its last character are both vowels ('a', 'e', 'i', 'o', 'u'). A one-letter word that is a vowel counts.
For each query, count the vowel strings among words[l] through words[r] inclusive. Return an array of answers in the same order as queries.
02 · Examples
words = ["ice","tree","ozone","u","eye"], queries = [[0,4],[1,2],[1,1]]
[4,1,0]
ice, ozone, u and eye start and end with a vowel; tree does not. Range [0,4] holds 4 such words, [1,2] holds only ozone, and [1,1] holds only tree, giving 0.
words = ["apple","banana","orange"], queries = [[0,0],[0,2],[1,1]]
[1,2,0]
apple and orange qualify, banana does not. So the answers are 1, 2 and 0.
words = ["a","b","io"], queries = [[0,2],[2,2]]
[2,1]
A single-letter word like "a" both starts and ends with a vowel. "io" also qualifies, so [0,2] gives 2 and [2,2] gives 1.
03 · Constraints
- 011 <= words.length <= 105
- 021 <= words[i].length <= 40
- 03words[i] consists of lowercase English letters
- 041 <= queries.length <= 105
- 050 <= l <= r < words.length
04 · Optimal complexity
- Time
- O(n + q)
- Space
- O(n)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.