> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count Vowel Strings in Ranges Python Solution

> Tested Python solution for LeetCode 2559 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2559, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/count-vowel-strings-in-ranges/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2559   # by problem number
lcpy gen -s count_vowel_strings_in_ranges   # by problem name
```

## Problem

You are given a \<strong>0-indexed\</strong> array of strings \<code>words\</code> and a 2D array of integers \<code>queries\</code>.

Each query \<code>queries\[i] = \[l\<sub>i\</sub>, r\<sub>i\</sub>]\</code> asks us to find the number of strings present at the indices ranging from \<code>l\<sub>i\</sub>\</code> to \<code>r\<sub>i\</sub>\</code> (both \<strong>inclusive\</strong>) of \<code>words\</code> that start and end with a vowel.

Return \<em>an array \</em>\<code>ans\</code>\<em> of size \</em>\<code>queries.length\</code>\<em>, where \</em>\<code>ans\[i]\</code>\<em> is the answer to the \</em>\<code>i\</code>\<sup>th\</sup>\<em> query\</em>.

\<strong>Note\</strong> that the vowel letters are \<code>'a'\</code>, \<code>'e'\</code>, \<code>'i'\</code>, \<code>'o'\</code>, and \<code>'u'\</code>.

### Examples

```
Input: words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]]
Output: [2,3,0]
Explanation: The strings starting and ending with a vowel are "aba", "ece", "aa" and "e".
The answer to the query [0,2] is 2 (strings "aba" and "ece").
The answer to the query [1,4] is 3 (strings "ece", "aa", "e").
The answer to the query [1,1] is 0.
We return [2,3,0].
```

```
Input: words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]]
Output: [3,2,1]
Explanation: Every string satisfies the conditions, so we return [3,2,1].
```

### Constraints

* 1 \<= words.length \<= 10^5
* 1 \<= words\[i].length \<= 40
* words\[i] consists only of lowercase English letters.
* sum(words\[i].length) \<= 3 \* 10^5
* 1 \<= queries.length \<= 10^5
* 0 \<= l\<sub>i\</sub> \<= r\<sub>i\</sub> \< words.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_vowel_strings_in_ranges/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_vowel_strings_in_ranges/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + q)
    # Space: O(n)
    def vowel_strings(self, words: list[str], queries: list[list[int]]) -> list[int]:
        vowels = set("aeiou")
        prefix = [0]
        for word in words:
            is_vowel = word[0] in vowels and word[-1] in vowels
            prefix.append(prefix[-1] + int(is_vowel))
        return [prefix[right + 1] - prefix[left] for left, right in queries]
```

## Complexity

| Time | Space |
| - | - |
| O(n + q) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.