Find the Longest Substring Containing Vowels in Even Counts LeetCode solution in C++

1
// When the xor of all the even times numbers are done it results in 0. The xor
2
// of the vowels are done by indicating them with a single digit and the xor
3
// value is stored in a map
4
class Solution {
5
public:
6
int findTheLongestSubstring(string s) {
7
int x = 0;
8
unordered_map<int, int> mp;
9
mp[0] = -1;
10
int n = 0;
11
for (int i = 0; i < s.length(); i++) {
12
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
13
x ^= (s[i] - 'a' + 1);
14
if (mp.find(x) == mp.end()) mp[x] = i;
15
}
16
if (mp.find(x) != mp.end()) n = max(n, i - mp[x]);
17
}
18
return n;
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0