Count the Number of Vowel Strings in Range - Problem
Imagine you're a linguist analyzing a collection of words to find patterns in vowel usage. You have an array of strings and need to count how many words in a specific range are "vowel strings" - words that both start and end with vowel characters.
A vowel string is defined as a string that:
- Starts with a vowel character ('a', 'e', 'i', 'o', 'u')
- Ends with a vowel character ('a', 'e', 'i', 'o', 'u')
Given a 0-indexed array of strings words and two integers left and right, return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].
Example: For words = ["are", "amy", "u", "an", "i"] and range [0, 2], we check indices 0, 1, 2. The words "are" (starts 'a', ends 'e') and "u" (starts 'u', ends 'u') are vowel strings, so we return 2.
Input & Output
example_1.py โ Basic Range Check
$
Input:
words = ["are", "amy", "u", "an", "i"], left = 0, right = 2
โบ
Output:
2
๐ก Note:
In range [0,2], we check "are" (starts 'a', ends 'e' - both vowels โ), "amy" (starts 'a', ends 'y' - y is not a vowel โ), "u" (starts 'u', ends 'u' - both vowels โ). Total: 2 vowel strings.
example_2.py โ Full Range
$
Input:
words = ["hey", "aaa", "mu", "ooo", "artro"], left = 1, right = 4
โบ
Output:
3
๐ก Note:
In range [1,4], we check "aaa" (starts 'a', ends 'a' โ), "mu" (starts 'm', ends 'u' โ), "ooo" (starts 'o', ends 'o' โ), "artro" (starts 'a', ends 'o' โ). Total: 3 vowel strings.
example_3.py โ Single Element
$
Input:
words = ["programming", "education", "algorithm"], left = 1, right = 1
โบ
Output:
1
๐ก Note:
Range [1,1] contains only "education". It starts with 'e' and ends with 'n'. Since 'n' is not a vowel, this would be 0. Wait - let me recalculate: "education" ends with 'n', not a vowel, so the answer is 0. But "education" actually starts with 'e' and ends with 'n', so it's not a vowel string. The answer should be 0.
Constraints
- 1 โค words.length โค 1000
- 1 โค words[i].length โค 10
- words[i] consists of only lowercase English letters
- 0 โค left โค right < words.length
Visualization
Tap to expand
Understanding the Visualization
1
Setup Range
Identify the shelf range [left, right] you need to inspect
2
Check Each Book
For each book in range, look at the first and last letter of the title
3
Vowel Validation
Count books where both first and last letters are vowels (a,e,i,o,u)
4
Final Count
Return the total count of qualifying vowel-titled books
Key Takeaway
๐ฏ Key Insight: We only need to examine the first and last character of each string in the given range, making this a simple O(n) single-pass problem with O(1) space complexity.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code