The Find the Longest Substring Containing Vowels in Even Counts interview question asks you to find the length of the longest substring where each of the five English vowels ('a', 'e', 'i', 'o', 'u') appears an even number of times (0 is considered even). Consonants can appear any number of times.
This is a high-signal problem for companies like Microsoft, Meta, and Google. It tests your ability to combine Bit Manipulation with the Prefix Sum interview pattern. It evaluation whether you can represent the parity (even/odd) of multiple items as a single integer state (a bitmask) and use a Hash Map to find the earliest occurrence of that state to maximize the distance.
This problem uses a Bitmask Prefix State approach.
firstOccurrence where mask 0 is at index -1.currentMask (using XOR).currentMask has been seen before, the substring between the firstOccurrence[currentMask] and the current index has an even count of all vowels (because and ).currentIndex - firstOccurrence[currentMask].String: "eleetminicoworoep"
00010. firstOccurrence[00010] = 0.00000 (bit flipped back).mask 0 was at -1. Length = ("el").mask 0 at index -1, which misses valid substrings that start from the beginning of the string.Bitmasks are perfect for tracking "parity" or "existence" of a small set of items (like 5 vowels or 26 letters). Combining bitmasks with a Hash Map to store the "first seen" index is a classic Prefix Sum interview pattern for finding the longest subarray with a specific property.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Unique Length-3 Palindromic Subsequences | Medium | Solve | |
| Number of Wonderful Substrings | Medium | Solve | |
| Can Make Palindrome from Substring | Medium | Solve | |
| Palindrome Permutation | Easy | Solve | |
| Find Longest Awesome Substring | Hard | Solve |