Longest substring whose characters can be rearranged to form a Palindrome
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: S = “aabe”
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”.
Input: S = “adbabd”
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.
- Define a bitmask variable mask which tracks if the occurrence of each character is even or odd.
- Create a dictionary index that keeps track of the index of each bitmask.
- 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.
- 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.
- 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.
- 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.
- 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.
- Print the maximum length of substring after the above steps.
Below is the implementation of the above approach:
Time Complexity: O(N * 26)
Auxiliary Space: O(N * 26)