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]whereibelongs to the inclusive range[left, right].
Input: words =["are","amy","u"], left =0, right =2Output: 2Explanation:
-"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 is2.
Input: words =["hey","aeo","mu","ooo","artro"], left =1, right =4Output: 3Explanation:
-"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 is3.
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.
classSolution {
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;
}
};
classSolution {
publicintvowelStrings(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;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
funvowelStrings(words: Array<String>, left: Int, right: Int): Int {
val vowels = setOf('a','e','i','o','u')
var ans = 0for (i in left..right) {
val w = words[i]
if (w.first() in vowels && w.last() in vowels) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defvowelStrings(self, words: list[str], left: int, right: int) -> int:
vowels = {'a','e','i','o','u'}
ans =0for i in range(left, right+1):
w = words[i]
if w[0] in vowels and w[-1] in vowels:
ans +=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnvowel_strings(words: Vec<String>, left: i32, right: i32) -> i32 {
let vowels = [b'a', b'e', b'i', b'o', b'u'];
letmut ans =0;
for i in left..=right {
let w = words[i asusize].as_bytes();
if vowels.contains(&w[0]) && vowels.contains(&w[w.len()-1]) {
ans +=1;
}
}
ans
}
}