1473 Find The Longest Substring Containing Vowels In Even Counts
1473 Find The Longest Substring Containing Vowels In Even Counts
Find the Longest Substring Containing Vowels in Even Counts 
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:
1
2
3
4
5
**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:
1
2
3
4
5
**Input:** s = "leetcodeisgreat"
**Output:** 5
**Explanation:** The longest substring is "leetc" which contains two e's.
Example 3:
1
2
3
4
5
**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:
1
2
1 <= s.length <= 5 x 10^5
s contains only lowercase English letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def findTheLongestSubstring(self, s: str) -> int:
prefixXOR = 0
characterMap = [0] * 26
characterMap[ord("a") - ord("a")] = 1
characterMap[ord("e") - ord("a")] = 2
characterMap[ord("i") - ord("a")] = 4
characterMap[ord("o") - ord("a")] = 8
characterMap[ord("u") - ord("a")] = 16
mp = [-1] * 32
longestSubstring = 0
for i in range(len(s)):
prefixXOR ^= characterMap[ord(s[i]) - ord("a")]
if mp[prefixXOR] == -1 and prefixXOR != 0:
mp[prefixXOR] = i
longestSubstring = max(longestSubstring, i - mp[prefixXOR])
return longestSubstring
This post is licensed under CC BY 4.0 by the author.