> ## 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.

# Maximum Number of Vowels in a Substring of

> Tested Python solution for LeetCode 1456 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1456, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/).

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

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

## Problem

Given a string `s` and an integer `k`, return *the maximum number of vowel letters in any substring of* `s` *with length* `k`.

**Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

### Examples

```
Input: s = "abciiidef", k = 3
Output: 3
```

**Explanation:** The substring "iii" contains 3 vowel letters.

```
Input: s = "aeiou", k = 2
Output: 2
```

**Explanation:** Any substring of length 2 contains 2 vowels.

```
Input: s = "leetcode", k = 3
Output: 2
```

**Explanation:** "lee", "eet" and "ode" contain 2 vowels.

### Constraints

* 1 \<= s.length \<= 10^5
* s consists of lowercase English letters.
* 1 \<= k \<= s.length

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_vowels(self, s: str, k: int) -> int:
        vowels = frozenset("aeiou")
        count = sum(c in vowels for c in s[:k])
        best = count
        for i in range(k, len(s)):
            count += (s[i] in vowels) - (s[i - k] in vowels)
            if count > best:
                best = count
                if best == k:
                    return best
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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