Skip to content

2586. Count the Number of Vowel Strings in Range 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  int vowelStrings(vector<string>& words, int left, int right) {
    return count_if(words.begin() + left, words.begin() + right + 1,
                    [this](const string& word) {
      return isVowel(word.front()) && isVowel(word.back());
    });
  }

 private:
  bool isVowel(char c) {
    static constexpr string_view kVowels = "aeiou";
    return kVowels.find(c) != string_view::npos;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int vowelStrings(String[] words, int left, int right) {
    return (int) Arrays.asList(words)
        .subList(left, right + 1)
        .stream()
        .filter(word -> isVowel(word.charAt(0)) && isVowel(word.charAt(word.length() - 1)))
        .count();
  }

  private boolean isVowel(char c) {
    return "aeiou".indexOf(c) != -1;
  }
}
1
2
3
4
5
class Solution:
  def vowelStrings(self, words: list[str], left: int, right: int) -> int:
    kVowels = 'aeiou'
    return sum(word[0] in kVowels and word[-1] in kVowels
               for word in words[left:right + 1])