You are given a 0-indexed array of string words
and two integers left
and right
.
A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
.
Return the number of vowel strings words[i]
where i
belongs to the inclusive range [left, right]
.
Example 1:
Input: words = ["are","amy","u"], left = 0, right = 2 Output: 2 Explanation: - "are" is a vowel string because it starts with 'a' and ends with 'e'. - "amy" is not a vowel string because it does not end with a vowel. - "u" is a vowel string because it starts with 'u' and ends with 'u'. The number of vowel strings in the mentioned range is 2.
Example 2:
Input: words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4 Output: 3 Explanation: - "aeo" is a vowel string because it starts with 'a' and ends with 'o'. - "mu" is not a vowel string because it does not start with a vowel. - "ooo" is a vowel string because it starts with 'o' and ends with 'o'. - "artro" is a vowel string because it starts with 'a' and ends with 'o'. The number of vowel strings in the mentioned range is 3.
Constraints:
1 <= words.length <= 1000
1 <= words[i].length <= 10
words[i]
consists of only lowercase English letters.0 <= left <= right < words.length
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to figure out how many words in a list start and end with vowels within a given range. The brute force approach looks at each word individually within that range and directly checks if it meets our criteria.
Here's how the algorithm would work step-by-step:
def count_vowel_strings_in_range_brute_force(words, left, right):
vowel_count = 0
for index in range(left, right + 1):
word = words[index]
first_character = word[0]
last_character = word[-1]
# Check if the first letter is a vowel
if first_character in 'aeiou':
# Further check if the last letter is a vowel
if last_character in 'aeiou':
vowel_count += 1
return vowel_count
Instead of checking every word in the specified range, we use a clever mathematical trick. We can quickly calculate the number of vowel strings of a given length starting from 'a' and subtract a similar calculation starting after the given range's end to efficiently obtain the answer.
Here's how the algorithm would work step-by-step:
def count_vowel_strings(string_length: int) -> int:
# Use combinatorics: (n+k-1) choose (k-1), where n=5 (vowels), k=string_length
return (string_length + 4) * (string_length + 3) * (string_length + 2) * (string_length + 1) // 24
def vowel_strings_in_range(words: list[str], left: int, right: int) -> int:
vowels = "aeiou"
vowel_string_count = 0
# Iterate only through the relevant part of the given list
for i in range(left, right + 1):
word = words[i]
# Checks if a word starts and ends with vowels
if word[0] in vowels and word[-1] in vowels:
vowel_string_count += 1
return vowel_string_count
Case | How to Handle |
---|---|
words is null or empty | Return 0 immediately, as there are no strings to process. |
left or right are out of bounds (left < 0 or right >= words.length) | Adjust left and right to be within valid bounds of the array (e.g., Math.max(0, left), Math.min(words.length - 1, right)). |
left > right | Return 0 immediately, as the range is invalid. |
words array contains empty strings | Empty strings do not start or end with vowels, so they should not be counted. |
words array contains strings with non-alphabetic characters | The vowel check should only consider alphabetic characters; non-alphabetic characters are irrelevant. |
words array contains strings that are all vowels, consonants, or mixed. | The logic should correctly identify vowel strings regardless of the overall distribution of vowels and consonants. |
Large words array (performance) | The solution should iterate through the specified range of the array efficiently, with a time complexity of O(n) where n is the number of words in the range. |
Strings in words that consist entirely of vowels, or non-vowels | The logic for identifying vowel strings handles strings of any length that begin and end with vowels. |