Description
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:
1 <= s.length <= 5 x 10^5
s
Β contains only lowercase English letters.
Solution
Python3
class Solution:
def findTheLongestSubstring(self, s: str) -> int:
mp = {"a": 1, "e": 2, "i": 4, "o": 8, "u": 16}
d = {0 : -1}
res = n = 0
for i, x in enumerate(s):
if x in mp:
n ^= mp[x]
if n not in d:
d[n] = i
else:
res = max(res, i - d[n])
return res