Leetcode Problem 1371. Find the Longest Substring Containing Vowels in Even Counts

1371. Find the Longest Substring Containing Vowels in Even Counts

Leetcode Solutions

Bit Masking and Prefix Sum

  1. Initialize a mask variable to 0 and an array firstOccurrence with size 32 (2^5 for each vowel parity combination) filled with -1, except firstOccurrence[0] which is set to 0.
  2. Iterate through the string, updating the mask based on the current character.
  3. If the character is a vowel, flip the corresponding bit in the mask.
  4. If firstOccurrence[mask] is -1, set it to the current index + 1.
  5. Calculate the length of the current valid substring as the difference between the current index and firstOccurrence[mask].
  6. Update the result with the maximum length found so far.
  7. Return the result.
UML Thumbnail

Brute Force with Optimization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...