comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
Hard |
2221 |
Biweekly Contest 32 Q4 |
|
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
.
Example 1:
Input: s = "3242415" Output: 5 Explanation: "24241" is the longest awesome substring, we can form the palindrome "24142" with some swaps.
Example 2:
Input: s = "12345678" Output: 1
Example 3:
Input: s = "213123" Output: 6 Explanation: "213123" is the longest awesome substring, we can form the palindrome "231132" with some swaps.
Constraints:
1 <= s.length <= 105
s
consists only of digits.
According to the problem description, the characters in the "super awesome substring" can be swapped to obtain a palindrome string. Therefore, there is at most one digit character in the "super awesome substring" that appears an odd number of times, and the rest of the digit characters appear an even number of times.
We can use an integer
If the substring
So, we can use a hash table or array to record the first occurrence of all states
Finally, return the answer.
The time complexity is
class Solution:
def longestAwesome(self, s: str) -> int:
st = 0
d = {0: -1}
ans = 1
for i, c in enumerate(s):
v = int(c)
st ^= 1 << v
if st in d:
ans = max(ans, i - d[st])
else:
d[st] = i
for v in range(10):
if st ^ (1 << v) in d:
ans = max(ans, i - d[st ^ (1 << v)])
return ans
class Solution {
public int longestAwesome(String s) {
int[] d = new int[1024];
int st = 0, ans = 1;
Arrays.fill(d, -1);
d[0] = 0;
for (int i = 1; i <= s.length(); ++i) {
int v = s.charAt(i - 1) - '0';
st ^= 1 << v;
if (d[st] >= 0) {
ans = Math.max(ans, i - d[st]);
} else {
d[st] = i;
}
for (v = 0; v < 10; ++v) {
if (d[st ^ (1 << v)] >= 0) {
ans = Math.max(ans, i - d[st ^ (1 << v)]);
}
}
}
return ans;
}
}
class Solution {
public:
int longestAwesome(string s) {
vector<int> d(1024, -1);
d[0] = 0;
int st = 0, ans = 1;
for (int i = 1; i <= s.size(); ++i) {
int v = s[i - 1] - '0';
st ^= 1 << v;
if (~d[st]) {
ans = max(ans, i - d[st]);
} else {
d[st] = i;
}
for (v = 0; v < 10; ++v) {
if (~d[st ^ (1 << v)]) {
ans = max(ans, i - d[st ^ (1 << v)]);
}
}
}
return ans;
}
};
func longestAwesome(s string) int {
d := [1024]int{}
d[0] = 1
st, ans := 0, 1
for i, c := range s {
i += 2
st ^= 1 << (c - '0')
if d[st] > 0 {
ans = max(ans, i-d[st])
} else {
d[st] = i
}
for v := 0; v < 10; v++ {
if d[st^(1<<v)] > 0 {
ans = max(ans, i-d[st^(1<<v)])
}
}
}
return ans
}
function longestAwesome(s: string): number {
const d: number[] = Array(1024).fill(-1);
let [st, ans] = [0, 1];
d[0] = 0;
for (let i = 1; i <= s.length; ++i) {
const v = s.charCodeAt(i - 1) - '0'.charCodeAt(0);
st ^= 1 << v;
if (d[st] >= 0) {
ans = Math.max(ans, i - d[st]);
} else {
d[st] = i;
}
for (let v = 0; v < 10; ++v) {
if (d[st ^ (1 << v)] >= 0) {
ans = Math.max(ans, i - d[st ^ (1 << v)]);
}
}
}
return ans;
}