Find Longest Awesome Substring LeetCode solution in C++

1
class Solution {
2
public:
3
int longestAwesome(string s) {
4
unordered_map<int, int> map;
5
int mask = 0, maxL = 0;
6
map[mask] = -1;
7

8
for (int i = 0; i < s.size(); ++i) {
9
int ch = s[i] - '0';
10
mask ^= (1 << ch);
11

12
if (map.find(mask) != map.end()) {
13
maxL = max(maxL, i - map[mask]);
14
}
15

16
for (int x = 0; x <= 9; ++x) {
17
int newMask = mask ^ (1 << x);
18
if (map.find(newMask) != map.end()) {
19
maxL = max(maxL, i - map[newMask]);
20
}
21
}
22

23
if (map.find(mask) == map.end()) {
24
map[mask] = i;
25
}
26
}
27
return maxL;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0