Find Longest Awesome Substring - Problem
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.
Note: A palindrome is a string that reads the same forward and backward. For example, "racecar" and "aabbaa" are palindromes, while "hello" is not.
Input & Output
Example 1 — Basic Case
$
Input:
s = "3242415"
›
Output:
5
💡 Note:
The substring "24241" can be rearranged to form the palindrome "24142" since digits 2,4,1 each appear twice and 4 appears once, satisfying the palindrome condition.
Example 2 — Single Digit
$
Input:
s = "12345"
›
Output:
1
💡 Note:
No substring longer than 1 can form a palindrome since all digits are different. Any single digit forms a palindrome, so the answer is 1.
Example 3 — All Same Digits
$
Input:
s = "213213"
›
Output:
6
💡 Note:
The entire string can form a palindrome (e.g., "123321") since each digit 1,2,3 appears exactly twice, all even counts.
Constraints
- 1 ≤ s.length ≤ 105
- s consists only of digits.
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string of digits like "3242415"
2
Check Palindrome Rule
Substring can form palindrome if ≤ 1 digit has odd count
3
Find Maximum Length
Return length of longest valid awesome substring
Key Takeaway
🎯 Key Insight: Use bit manipulation to efficiently track which digits have odd counts across all substrings
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code