Largest substring containing vowels in even counts
Filed under Amazon
Free read 1 of 3Use a 5-bit integer where each bit represents the parity (odd/even) of a specific vowel.
The question
Find the length of the longest substring containing an even count of each vowel (a, e, i, o, u). If multiple exist, return all of them.
Constraints
- String contains only lowercase English letters
- Length up to 10^5
- Vowels: a, e, i, o, u
Examples
- Input
- eleetminicoworo
- Output
- 7
- Why
- The substring 'eetmini' has 2 'e', 2 'i', 1 'a' (Wait, counts must be even).
Approach
Use a 5-bit integer where each bit represents the parity (odd/even) of a specific vowel.
Store the first occurrence index of each bitmask in a hash map.
As you iterate, if the current mask has been seen, the distance between current index and first occurrence is a candidate length.
Tips
- What they are testing: Use a bitmask to represent the parity of vowel counts.
- Common mistake: Re-calculating counts for every substring instead of using prefix XOR/bitmask.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.