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 substring "leetminicoworoep" contains: a:0, e:4, i:2, o:4, u:0. All vowels appear an even number of times, giving length 13.
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
Find Longest Substring with Even Vowel Countse l e e t m i n i c o w o r o e pInput: "eleetminicoworoep" (length 17)l e e t m i n i c o w o r o e pLongest valid substring: "leetminicoworoep" (length 13)Vowel counts: a=0, e=4, i=2, o=4, u=0 (all even ✓)Key Insight: Bitmask State TrackingSame parity state at two positions =valid substring between themOutput: 13
Understanding the Visualization
1
Input Analysis
String "eleetminicoworoep" with vowels to track
2
State Tracking
Track vowel count parity using bitmask
3
Result
Find longest substring with all even vowel counts
Key Takeaway
🎯 Key Insight: Use bitmask to track vowel parity - when same pattern repeats, substring between has all even vowel counts
Asked in
Facebook 8 Amazon 5 Microsoft 3
22.9K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen