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
.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Solution
Method 1 - Using bitmask and hashmap
Here is the approach:
To identify the length of the maximum awesome
substring that can be made a palindrome by swapping characters in s
, we need to leverage the properties of palindromes:
- Palindrome Properties:
- A string is a palindrome if each character appears an even number of times, except for at most one character which can appear an odd number of times.
- Use Bitmasking:
- Use a bitmask to represent the parity (odd/even) of character counts. This way, each bit in the bitmask corresponds to a character (
a
toz
), indicating whether the character’s count is odd or even. - Compute the state (bitmask) as we iterate through the string. This helps track the parity of character counts up to each position.
- Use a bitmask to represent the parity (odd/even) of character counts. This way, each bit in the bitmask corresponds to a character (
- HashMap for Previous States:
- Use a HashMap to store the first occurrence of each bitmask. This helps in quickly calculating the maximum length of substrings with specific properties.
- If we encounter the same bitmask again, it means the characters in between can form a palindrome.
- Check for Single Odd Character:
- Besides checking for exact bitmask parity, also check for each bit deviation to allow for one odd character (using
mask ^ (1 << i)
).
- Besides checking for exact bitmask parity, also check for each bit deviation to allow for one odd character (using
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the length of the strings
. Each character is processed once, updating and checking the bitmask. - 🧺 Space complexity:
O(1)
for the bitmask and auxiliary variables, but the HashMap can grow up toO(n)
in the worst case.