The Indian Engineer

Problem 1371 Find the Longest Substring Containing Vowels in Even Counts

Posted on 6 mins

Hash-Table String Bit-Manipulation Prefix-Sum

Problem Statement

Link - Problem 1371

Question

Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.

Example 1:

Input: s = "eleetminicoworoep"
Output: 13
Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.

Example 2:

Input: s = "leetcodeisgreat"
Output: 5
Explanation: The longest substring is "leetc" which contains two e's.

Example 3:

Input: s = "bcbcbc"
Output: 6
Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.

Constraints:

Solution

class Solution {
public:
    int findTheLongestSubstring(string s) {
        int prefixXOR = 0;
        int characterMap[26] = {0};
        characterMap['a' - 'a'] = 1;
        characterMap['e' - 'a'] = 2;
        characterMap['i' - 'a'] = 4;
        characterMap['o' - 'a'] = 8;
        characterMap['u' - 'a'] = 16;
        vector<int> mp(32, -1);
        int longestSubstring = 0;

        for (int i = 0; i < s.length(); i++) {
            prefixXOR ^= characterMap[s[i] - 'a'];
            if (mp[prefixXOR] == -1 and prefixXOR != 0) mp[prefixXOR] = i;

            longestSubstring = max(longestSubstring, i - mp[prefixXOR]);
        }

        return longestSubstring;
    }
};

Complexity Analysis

| Algorithm  | Time Complexity | Space Complexity |
| ---------- | --------------- | ---------------- |
| Prefix XOR | O(n)            | O(1)             |

Explanation

Intial Thoughts

The problem seems to involve tracking the occurrence of vowels in a string and finding the longest substring where each vowel appears an even number of times. It can be approached by maintaining a counter for each vowel or using a clever encoding scheme. An initial step could be to identify the possible encoding schemes for the vowels, such as using bits to represent each vowel. Another thought is to use a prefix sum or prefix XOR to efficiently track the occurrence of vowels. Breaking down the problem into smaller sub-problems, such as finding the longest substring ending at each position, could also be a viable strategy.

Intuitive Analysis

One way to intuitively solve this problem is to think of it as finding a ‘balance’ in the string where the counts of vowels are even. Using a bit encoding scheme, where each vowel is associated with a bit, can help in efficiently tracking this balance. The XOR operation can then be used to ‘cancel out’ the occurrence of vowels, effectively finding substrings where each vowel appears an even number of times. Another intuitive approach is to consider the problem as finding a ‘matching’ between the start and end of the substring, where the counts of vowels are balanced. This can be visualized as a ‘journey’ through the string, where the goal is to find the longest path with balanced vowel counts.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Represent the counts (odd or even) of vowels with a bitmask.

Precompute the prefix xor for the bitmask of vowels and then get the longest valid substring.