Leetcode 1371: Find the Longest Substring Containing Vowels in Even Counts

grid47
grid47
Exploring patterns and algorithms
Jun 22, 2024 6 min read

Given a string s, find the size of the longest substring containing each vowel (‘a’, ’e’, ‘i’, ‘o’, ‘u’) an even number of times.
Problem
Approach
Steps
Complexity
Input: The input consists of a single string s, containing only lowercase English letters.
Example: s = 'amazingtime'
Constraints:
• 1 <= s.length <= 5 * 10^5
• s consists of lowercase English letters.
Output: Return the size of the longest substring where each vowel ('a', 'e', 'i', 'o', 'u') appears an even number of times.
Example: 6
Constraints:
• The output will be a single integer.
Goal: To find the longest substring where vowels appear an even number of times.
Steps:
• Iterate through the string while keeping track of the count of each vowel.
• Use a bitmask to represent the count of each vowel appearing in the string so far.
• Track the first occurrence of each bitmask in a map and calculate the longest substring where the bitmask repeats.
Goal: Ensure the solution is efficient enough for large input sizes.
Steps:
• The string length will be at most 500,000.
Assumptions:
• The string is non-empty and contains only lowercase English letters.
Input: s = 'amazingtime'
Explanation: The longest substring 'amazi' contains two 'a's and two 'i's, so both appear an even number of times.

Input: s = 'applepie'
Explanation: The substring 'appl' contains an even number of 'e's, so the result is 5.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus