Input: str = “geeksforgeeks”
Explanation: eegksfskgee is the longest palindromic substring after rearranging the string.
Therefore, the required output is eegksfskgeeor.
Input: str = “engineering”
Approach: The problem can be solved using Hashing. The idea is to count the frequency of each character of the given string. If the count of occurrence of a character exceeds 1, append half(floor value) of its frequency on the left side of the resultant string and the remaining half on the right side of the resultant string. For the remaining characters, append one character in the middle of the resultant string and the rest either at the beginning or at the end of the resultant string. Follow the steps below to solve the problem:
- Initialize an array, say hash to store the frequency of each character.
- To efficiently append the characters on both sides of the resultant string, initialize three strings res1, res2, and res3.
- The string res1 stores the left half of the longest possible palindromic substring, res2 stores the right half of the longest possible palindromic substring, and res3 stores the remaining characters.
- Traverse the hash array and for the character, say hash[i], check if its frequency is greater than 1 or not. If found to be true, append the character floor(hash[i]/2) times in res1 and floor(hash[i]/2) times in res2.
- Otherwise, append the first character to not satisfy the above condition to res1 and all the remaining such characters to res3.
- Finally, return the string res1 + reverse(res2) + res3.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Minimum replacements required to obtain a K-periodic palindromic string
- Longest palindromic string possible after removal of a substring
- Minimum length of substring whose rotation generates a palindromic substring
- Rearrange array to obtain maximum possible value of concatenation of prefix GCDs
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Suffix Tree Application 6 - Longest Palindromic Substring
- Rearrange the string to maximize the number of palindromic substrings
- Make palindromic string non-palindromic by rearranging its letters
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Minimum size substring to be removed to make a given string palindromic
- Count characters to be shifted from the start or end of a string to obtain another string
- Find length of longest subsequence of one string which is substring of another string
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.