LeetCode solutions

2691. Count Vowel Strings in Ranges

My accepted Python solution to LeetCode problem 2691, Count Vowel Strings in Ranges, running in 25ms.

  • Difficulty: Medium
  • Python
  • Runtime 25ms
  • Memory 48.1MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 25ms, memory 48.1MB, accepted 2026-01-02.

python
class Solution:
    def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
        vowels = {'a', 'e', 'i', 'o', 'u'}
        
        # Check if a word starts and ends with a vowel
        def is_vowel_string(word):
            return word[0] in vowels and word[-1] in vowels
        
        # Build prefix sum array
        n = len(words)
        prefix = [0] * (n + 1)
        for i in range(n):
            prefix[i + 1] = prefix[i] + (1 if is_vowel_string(words[i]) else 0)
        
        # Answer queries using prefix sum
        result = []
        for l, r in queries:
            result.append(prefix[r + 1] - prefix[l])
        
        return result

Source