Show problems as
Classic problem #2559MediumArrayStringPrefix Sum

Count Vowel Strings in Ranges

Real-world version: Vowel-Bounded Labels in Each Stretch
Tracked in this browser only
Read Count Vowel Strings in Ranges on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Labels are given as words, and each entry of queries is a pair [l, r] naming a stretch of the list, ends included.

A label is vowel-bounded when both its first and its last character are vowels, that is one of 'a', 'e', 'i', 'o' or 'u'. A label of one character counts when that character is a vowel.

Return, for each stretch in turn, how many vowel-bounded labels it holds.

Examples

Example 1

Input
words = ["opal", "brick", "aria", "echo", "damp"], queries = [[0, 4], [1, 3], [2, 2], [4, 4]]
Output
[2, 2, 1, 0]

"aria" and "echo" both begin and end with a vowel; "opal" ends in a consonant, and "brick" and "damp" begin with one. So the whole list holds two, the middle stretch holds two, and the last two stretches hold one and none.

Example 2

Input
words = ["ba"], queries = [[0, 0]]
Output
[0]

The label "ba" begins with a consonant, so it does not count even though it ends with a vowel.

Example 3

Input
words = ["a"], queries = [[0, 0]]
Output
[1]

A label of one character counts when that character is a vowel, so it is both the first and the last.

Constraints

  • 1 <= words.length <= 10^5
  • 1 <= words[i].length <= 40
  • Every entry of words consists of lowercase English letters only
  • 1 <= queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= queries[i][0] <= queries[i][1] <= words.length - 1

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def vowel_strings(words: list[str], queries: list[list[int]]) -> list[int]:
Java
public int[] vowelStrings(String[] words, int[][] queries)