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.
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)
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.
- Check if characters of a given string can be rearranged to form a palindrome
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if a string can be rearranged to form special palindrome
- Check if characters of each word can be rearranged to form an Arithmetic Progression (AP)
- Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Longest substring with atmost K characters from the given set of characters
- Minimum length of substring whose rotation generates a palindromic substring
- Length of the longest substring that do not contain any palindrome
- Length of Longest Palindrome Substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Count pair of strings whose concatenation of substrings form a palindrome
- Rearrange characters to form palindrome if possible
- Check if the characters in a string form a Palindrome in O(1) extra space
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Length of the longest substring without repeating characters
- Find the longest substring with k unique characters in a given string
- Longest substring of only 4's from the first N characters of the infinite string
- Longest substring such that no three consecutive characters are same
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.