Count Vowel Strings in Ranges - Problem

Imagine you're working with a text analysis system that needs to quickly answer queries about specific word patterns. You have an array of words and need to efficiently handle multiple range queries.

Given a 0-indexed array of strings words and a 2D array of integers queries, your task is to count how many words in each specified range start and end with a vowel.

Each query queries[i] = [li, ri] asks: "How many strings at indices from li to ri (both inclusive) start and end with a vowel?"

Return an array ans where ans[i] is the answer to the i-th query.

Note: Vowels are 'a', 'e', 'i', 'o', and 'u' (case-sensitive).

Input & Output

example_1.py โ€” Basic Case
$ Input: words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
โ€บ Output: [2,3,0]
๐Ÿ’ก Note: Query [0,2]: "aba"(โœ“), "bcb"(โœ—), "ece"(โœ“) โ†’ count = 2. Query [1,4]: "bcb"(โœ—), "ece"(โœ“), "aa"(โœ“), "e"(โœ“) โ†’ count = 3. Query [1,1]: "bcb"(โœ—) โ†’ count = 0.
example_2.py โ€” Single Character
$ Input: words = ["a","e","i"], queries = [[0,2],[0,1]]
โ€บ Output: [3,2]
๐Ÿ’ก Note: All single vowel characters start and end with vowels. Query [0,2]: all 3 words qualify. Query [0,1]: first 2 words qualify.
example_3.py โ€” No Matches
$ Input: words = ["hello","world","coding"], queries = [[0,2],[1,2]]
โ€บ Output: [0,0]
๐Ÿ’ก Note: None of the words start and end with vowels: "hello"(consonant-vowel), "world"(consonant-consonant), "coding"(consonant-consonant).

Constraints

  • 1 โ‰ค words.length โ‰ค 105
  • 1 โ‰ค words[i].length โ‰ค 40
  • words[i] consists only of lowercase English letters
  • 1 โ‰ค queries.length โ‰ค 3 ร— 105
  • queries[i] = [li, ri]
  • 0 โ‰ค li โ‰ค ri < words.length

Visualization

Tap to expand
Prefix Sum ApproachStep 1: Identify Vowel-Bounded Words"aba"โœ“ Vowel"bcb"โœ— Consonant"ece"โœ“ Vowel"aa"โœ“ Vowel"e"โœ“ VowelIndex 0Index 1Index 2Index 3Index 4Step 2: Build Prefix Sum Array011234prefix[0]prefix[1]prefix[2]prefix[3]prefix[4]prefix[5]Step 3: Answer Range Queries in O(1)Query [1,4]: How many vowel strings from index 1 to 4?Answer = prefix[5] - prefix[1] = 4 - 1 = 3Range [1,4]๐ŸŽฏ Key Insight: Range Sum = prefix[right+1] - prefix[left]
Understanding the Visualization
1
Scan and Mark
Go through each word, check if it starts and ends with vowels
2
Build Prefix
Create cumulative sum: prefix[i] = total count from start to position i-1
3
Range Query
For range [l,r]: answer = prefix[r+1] - prefix[l]
4
Instant Result
Each query answered in O(1) time using simple arithmetic
Key Takeaway
๐ŸŽฏ Key Insight: Prefix sums transform O(N) range queries into O(1) arithmetic operations, making this approach optimal for multiple queries.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 25
67.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen