Count the Number of Vowel Strings in Range
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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 stringswords[i]wherei belongs to the inclusive range[left, right].
Examples
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 <= 10001 <= words[i].length <= 10words[i]consists of only lowercase English letters.0 <= left <= right < words.length
Solution
Method 1 – Simple Iteration and Vowel Check
Intuition
A string is a vowel string if it starts and ends with a vowel. We can simply check each word in the given range and count how many satisfy this property.
Approach
- Define a set of vowels: {'a', 'e', 'i', 'o', 'u'}.
- Iterate over the indices from left to right (inclusive).
- For each word, check if its first and last character are both vowels.
- Count and return the number of such words.
Code
C++
class Solution {
public:
int vowelStrings(vector<string>& words, int left, int right) {
auto isVowel = [](char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
};
int ans = 0;
for (int i = left; i <= right; ++i) {
if (isVowel(words[i][0]) && isVowel(words[i].back())) ++ans;
}
return ans;
}
};
Go
func vowelStrings(words []string, left int, right int) int {
isVowel := func(c byte) bool {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
}
ans := 0
for i := left; i <= right; i++ {
w := words[i]
if isVowel(w[0]) && isVowel(w[len(w)-1]) {
ans++
}
}
return ans
}
Java
class Solution {
public int vowelStrings(String[] words, int left, int right) {
Set<Character> vowels = Set.of('a','e','i','o','u');
int ans = 0;
for (int i = left; i <= right; i++) {
String w = words[i];
if (vowels.contains(w.charAt(0)) && vowels.contains(w.charAt(w.length()-1))) ans++;
}
return ans;
}
}
Kotlin
class Solution {
fun vowelStrings(words: Array<String>, left: Int, right: Int): Int {
val vowels = setOf('a','e','i','o','u')
var ans = 0
for (i in left..right) {
val w = words[i]
if (w.first() in vowels && w.last() in vowels) ans++
}
return ans
}
}
Python
class Solution:
def vowelStrings(self, words: list[str], left: int, right: int) -> int:
vowels = {'a','e','i','o','u'}
ans = 0
for i in range(left, right+1):
w = words[i]
if w[0] in vowels and w[-1] in vowels:
ans += 1
return ans
Rust
impl Solution {
pub fn vowel_strings(words: Vec<String>, left: i32, right: i32) -> i32 {
let vowels = [b'a', b'e', b'i', b'o', b'u'];
let mut ans = 0;
for i in left..=right {
let w = words[i as usize].as_bytes();
if vowels.contains(&w[0]) && vowels.contains(&w[w.len()-1]) {
ans += 1;
}
}
ans
}
}
TypeScript
class Solution {
vowelStrings(words: string[], left: number, right: number): number {
const vowels = new Set(['a','e','i','o','u'])
let ans = 0
for (let i = left; i <= right; ++i) {
const w = words[i]
if (vowels.has(w[0]) && vowels.has(w[w.length-1])) ans++
}
return ans
}
}
Complexity
- ⏰ Time complexity:
O(n * m)where n is the number of words in the range and m is the average word length (for string indexing). - 🧺 Space complexity:
O(1)as only a few variables and a set are used.