Leetcode 2586. Count the Number of Vowel Strings in Range
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 (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) and ends with a vowel (‘a’, ‘e’, ‘i’, ‘o’, ‘u’).
Return the number of vowel strings words[i]
where left <= i <= right
.
left
and right
inclusive?
[left, right]
inclusive.words
array?
0
in that case.left
and right
guaranteed to be within the bounds of the array?
left
and right
will be valid indices within the bounds of the array.left
to right
(inclusive).[left, right]
. Each check (whether the first and last characters of a string are vowels) is an O(1) operation.#include <vector>
#include <string>
#include <unordered_set>
class Solution {
public:
int countVowelStringsInRange(std::vector<std::string>& words, int left, int right) {
std::unordered_set<char> vowels {'a', 'e', 'i', 'o', 'u'};
int count = 0;
for (int i = left; i <= right; ++i) {
const std::string& word = words[i];
if (!word.empty() && vowels.count(word.front()) && vowels.count(word.back())) {
++count;
}
}
return count;
}
};
In this implementation:
[left, right]
and check if the first and last characters of each word are in the vowel set.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?