[LeetCode] Find Longest Awesome Substring

1542. Find Longest Awesome Substring

You are given a string s. An awesome substring is a non-empty substring of s such that we can make any number of swaps in order to make it a palindrome.

Return the length of the maximum length awesome substring of s.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int longestAwesome(string s) {
int n = s.length(), res = 0;
unordered_map<int, int> lookup{{0,-1}};

for(int i = 0, dp = 0; i < n; i++) {
dp ^= (1<<(s[i] & 0b1111));

if(lookup.count(dp))
res = max(res, i - lookup[dp]);
else lookup[dp] = i;

for(int j = 0; j < 10; j++) {
int submask = dp ^ (1<<j);
if(lookup.count(submask))
res = max(res, i - lookup[submask]);
}
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/03/PS/LeetCode/find-longest-awesome-substring/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.