Count Vowel Strings in Ranges

You are given a 0-indexed array of strings `words` and a 2D array of integers `queries`. Each query `queries[i] = [l_i, r_i]` asks for the number of strings in the range `l_i` to `r_i` (both inclusive) of `words` that start and end with a vowel. Return an array `ans` of size `queries.length`, where `ans[i]` is the answer to the `i`-th query. The vowel letters are `a`, `e`, `i`, `o` and `u`.

30:00

Count Vowel Strings in Ranges
medium
Free written solution
Topics
Companies

You are given a 0-indexed array of strings words and a 2D array of integers queries.

Each query queries[i] = [l_i, r_i] asks for the number of strings in the range l_i to r_i (both inclusive) of words that start and end with a vowel.

Return an array ans of size queries.length, where ans[i] is the answer to the i-th query. The vowel letters are a, e, i, o and u.

Example 1:
Input: {"words":["aba","bcb","ece","aa","e"],"queries":[[0,2],[1,4],[1,1]]}
Output: [2,3,0]
Constraints:
  • 1≤words.length≤1051 \leq \text{words.length} \leq 10^5

  • 1≤words[i].length≤401 \leq \text{words}[i].\text{length} \leq 40

  • words[i] consists only of lowercase English letters.

  • sum(words[i].length)≤3⋅105\text{sum}(\text{words}[i].\text{length}) \leq 3 \cdot 10^5

  • 1≤queries.length≤1051 \leq \text{queries.length} \leq 10^5

  • 0≤li≤ri<words.length0 \leq l_i \leq r_i < \text{words.length}

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"words":["aba","bcb","ece","aa","e"],"queries":[[0,2],[1,4],[1,1]]}

Answering each query by scanning its range could cost 5 checks per query. Instead, mark which words start and end with a vowel and build prefix counts once: prefix[0] = 0.

words (vowel / consonant ends)

aba

0

bcb

1

ece

2

aa

3

e

4

prefix count of good words

04

ans (0/3)

no queries answered yet

0/5 words checked · 0 good so far

Variables
VariableValue
prefix[0]0
DepthFunction Call
Stack empty
0/10