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

# Find the Longest Substring Containing Vowels

> Tested Python solution for LeetCode 1371 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 1371, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Bit Manipulation](/catalog/topics/bit-manipulation), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/description/).

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

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

## Problem

Given the string \<code>s\</code>, return the size of the longest substring containing each vowel an even number of times. That is, \<code>'a'\</code>, \<code>'e'\</code>, \<code>'i'\</code>, \<code>'o'\</code>, and \<code>'u'\</code> must appear an even number of times.

### Examples

```
Input: s = "eleetminicoworoep"
Output: 13
Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.
```

```
Input: s = "leetcodeisgreat"
Output: 5
Explanation: The longest substring is "leetc" which contains two e's.
```

```
Input: s = "bcbcbc"
Output: 6
Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.
```

### Constraints

* 1 \<= s.length \<= 5 x 10^5
* s contains only lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) (at most 32 distinct masks)
    def find_the_longest_substring(self, s: str) -> int:
        # Prefix XOR bitmask over the 5 vowels; two equal prefix masks
        # bracket a substring with all-even vowel counts.
        first_seen = {0: -1}
        mask = 0
        best = 0
        for i, ch in enumerate(s):
            if ch == "a":
                mask ^= 1
            elif ch == "e":
                mask ^= 2
            elif ch == "i":
                mask ^= 4
            elif ch == "o":
                mask ^= 8
            elif ch == "u":
                mask ^= 16
            if mask in first_seen:
                best = max(best, i - first_seen[mask])
            else:
                first_seen[mask] = i
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) (at most 32 distinct masks) |

## Tags

[NeetCode All](/catalog/neetcode).


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