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
Library Shelf InspectorShelf Range [0, 2]"are"Index: 0โœ“First: 'a' (vowel)Last: 'e' (vowel)"amy"Index: 1โœ—First: 'a' (vowel)Last: 'y' (consonant)"u"Index: 2โœ“First: 'u' (vowel)Last: 'u' (vowel)"an"Index: 3Outside Range"i"Index: 4Outside RangeVowel Check Process1. vowels = {a, e, i, o, u}2. For each word in range:โ€ข Check first characterโ€ข Check last characterโ€ข Both vowels? count++2Vowel Strings Found
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.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
31.2K Views
Medium Frequency
~8 min Avg. Time
845 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