Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 <= 105s consists only of digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Bit Manipulation
"3242415"
"12345678"
"213123"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1542: Find Longest Awesome Substring
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;
}
}
// Accepted solution for LeetCode #1542: Find Longest Awesome Substring
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
}
# Accepted solution for LeetCode #1542: Find Longest Awesome Substring
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
// Accepted solution for LeetCode #1542: Find Longest Awesome Substring
struct Solution;
use std::collections::HashMap;
impl Solution {
fn longest_awesome(s: String) -> i32 {
let mut mask = 0;
let mut hm: HashMap<u32, usize> = HashMap::new();
hm.insert(0, 0);
let mut res = 0;
for (i, b) in s.bytes().enumerate() {
mask ^= 1 << (b - b'0');
if let Some(j) = hm.get(&mask) {
res = res.max(i + 1 - j);
}
for k in 0..10 {
if let Some(j) = hm.get(&(mask ^ (1 << k))) {
res = res.max(i + 1 - j);
}
}
hm.entry(mask).or_insert(i + 1);
}
res as i32
}
}
#[test]
fn test() {
let s = "3242415".to_string();
let res = 5;
assert_eq!(Solution::longest_awesome(s), res);
let s = "12345678".to_string();
let res = 1;
assert_eq!(Solution::longest_awesome(s), res);
let s = "213123".to_string();
let res = 6;
assert_eq!(Solution::longest_awesome(s), res);
let s = "00".to_string();
let res = 2;
assert_eq!(Solution::longest_awesome(s), res);
}
// Accepted solution for LeetCode #1542: Find Longest Awesome Substring
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;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.