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
Find Longest Awesome Substring: "3242415"3242415Awesome Substring: "24241" (length 5)Frequency: 2→2, 4→2, 1→1 (at most one odd count)Can rearrange to palindrome: "24142"Output: 5Maximum length awesome substring found
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
Asked in
Google 25 Facebook 20 Amazon 15
23.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen