# Longest substring whose characters can be rearranged to form a Palindrome

• Difficulty Level : Hard
• Last Updated : 07 Jun, 2021

Given a string S of length N which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome

Examples:

Input: S = āaabeā
Output: 3
Explanation:
The substring āaabā can be rearranged to form “aba”, which is a palindromic substring.
Since the length of āaabā is 3 so output is 3.
Notice that “a”, “aa”, “b” and “e” can be arranged to form palindromic strings, but they are not longer than āaabā.

Output: 6
Explanation:
The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is “abddba”.
Therefore, output length of the string i.e., 6.

Naive Approach: The idea is to generate all possible substring and keep count of each character in it. Initialize answer with value 0. If the count of each character is even with at most one character with the odd occurrence, the substring can be re-arranged to form palindromic string. If a substring satisfy this property then update the answer. Print the maximum length after the above steps.

Time Complexity: O(N3 * 26)
Auxiliary Space: O(N2 * 26)

Efficient Approach: The idea is to observe that the string is a palindrome if at most one character occurs an odd number of times. So there is no need to keep the total count of each character. Just knowing that it is occurring even or an odd number of times is enough. To do this, use bit masking since the count of lowercase alphabets is only 26.

1. Define a bitmask variable mask which tracks if the occurrence of each character is even or odd.
2. Create a dictionary index that keeps track of the index of each bitmask.
3. Traverse the given string S. First, convert the characters from ‘a’ – ‘z’ to 0 – 25 and store this value in a variable temp. For each occurrence of the character, take Bitwise XOR of 2temp with the mask.
4. If the character occurs even number of times, its bit in the mask will be off else it will be on. If the mask is currently not in the index, simply assign present index i to bitmask mask in the index.
5. If the mask is present in the index it means that from the index[mask] to i, the occurrence of all characters is even which is suitable for a palindromic substring. Therefore, update the answer if the length of this segment from the index[mask] to i is greater than the answer.
6. To check for the substring with one character occurring an odd number of times, iterate a variable j over [0, 25]. Store Bitwise XOR of x with 2j in mask2
7. If mask2 is present in the index, this means that this character is occurring an odd number of times and all characters occur even a number of times in the segment index[mask2] to i, which is also a suitable condition for a palindromic string. Therefore, update our answer with the length of this substring if it is greater than the answer.
8. Print the maximum length of substring after the above steps.

Below is the implementation of the above approach:

## Javascript


Output:
6

Time Complexity: O(N * 26)
Auxiliary Space: O(N * 26)

My Personal Notes arrow_drop_up