mediumBit ManipulationStringSliding Window

Find Longest Substring With Even Vowel Counts

mediumTime: O(n)Space: O(1)

Signals to notice

longest substring with all vowels appearing even timesbitmask parity trackingprefix XOR

Brute force first

Check every substring — O(n²).

The key insight

5-bit bitmask tracks vowel parity. Same bitmask at positions i,j means even counts between them. Store first occurrence. O(n).

What must stay true

Same bitmask = same parity state = vowels toggled even times between. Length = j - firstOccurrence[mask].

Easy way to go wrong

Not initializing mask 0 at position -1 — needed for substrings starting at index 0.

Sliding Window Pattern