Find the Longest Substring Containing Vowels in Even Counts

MediumBit Manipulationprefix sum

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a lowercase string s, find the length of the longest substring in which each of the five vowels 'a', 'e', 'i', 'o', and 'u' appears an **even** number of times. Zero occurrences counts as even. Return that length. If no non-empty substring qualifies, return 0 (the empty substring always qualifies trivially). Target O(n) time.

Examples

Input: s = "eleetminicoworoep"
Output: 13
The substring leetminicowor has two e's, two i's, and two o's, and no a or u.
Input: s = "leetcodeisgreat"
Output: 5
The substring leetc contains e twice and no other vowel.
Input: s = "bcbcbc"
Output: 6
There are no vowels at all, so the whole string qualifies.

Constraints

  • 1 <= s.length <= 5 * 10^5
  • s consists of lowercase English letters only

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More prefix sum problems