1542. Find Longest Awesome Substring
Solutions
class Solution {
public:
int longestAwesome(string s) {
unordered_map<int, int> pos; pos[0] = -1;
int state = 0, res = 0;
for (int i = 0; i < s.size(); i++) {
int d = s[i] - '0';
state ^= (1 << d);
// count the first appearance of each state
if (!pos.count(state))
pos[state] = i;
else
res = max(res, i - pos[state]);
// check for the second case
for (int d = 0; d <= 9; d++) {
int tmp = state ^ (1 << d);
if (pos.count(tmp))
res = max(res, i - pos[tmp]);
}
}
return res;
}
};Last updated