Find the Longest Substring Containing Vowels in Even Counts - Problem
Given a string s, return the length of the longest substring containing each vowel ('a', 'e', 'i', 'o', 'u') an even number of times.
A substring is a contiguous sequence of characters within a string. For the substring to be valid, every vowel must appear an even number of times (including zero times).
Note: Consonants can appear any number of times and don't affect the validity of the substring.
Input & Output
Example 1 — Basic Case
$
Input:
s = "eleetminicoworoep"
›
Output:
13
💡 Note:
The longest valid substring has length 13. Using the bit manipulation approach, we track vowel parity states and find the longest substring between two positions with the same parity state for all vowels.
Example 2 — No Vowels
$
Input:
s = "bcbcbc"
›
Output:
6
💡 Note:
The entire string has no vowels, so all vowels appear 0 times (even). The full length 6 is the answer.
Example 3 — All Odd Counts
$
Input:
s = "aeiou"
›
Output:
0
💡 Note:
Each vowel appears exactly once (odd count). No valid substring exists where all vowels have even counts.
Constraints
- 1 ≤ s.length ≤ 5 × 104
- s consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code