Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message “no palindromic string”.
Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string
1. Sort the string characters in alphabetical(ascending) order.
2. One be one find lexicographically next permutation of the given string.
3. The first permutation which is palindrome is the answer.
Efficient Approach: Properties for palindromic string:
1. If length of string is even, then the frequency of each character in the string must be even.
2. If the length is odd then there should be one character whose frequency is odd and all other chars must have even frequency and at-least one occurrence of the odd character must be present in the middle of the string.
1. Store frequency of each character in the given string
2. Check whether a palindromic string can be formed or not using the properties of palindromic string mentioned above.
3. If palindromic string cannot be formed, return “No Palindromic String”.
4. Else we create three strings and then return front_str + odd_str + rear_str.
- odd_str : It is empty if there is no character with odd frequency. Else it contains all occurrences of odd character.
- front_str : Contains half occurrences of all even occurring characters of string in increasing order.
- rear_str Contains half occurrences of all even occurring characters of string in reverse order of front_str.
Below is implementation of above steps.
Time Complexity : O(n) where n is length of input string. Assuming that size of string alphabet is constant.
This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Lexicographically all Shortest Palindromic Substrings from a given string
- Find the lexicographically largest palindromic Subsequence of a String
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Make palindromic string non-palindromic by rearranging its letters
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find the count of palindromic sub-string of a string in its sorted form
- Find a palindromic string B such that given String A is a subsequense of B
- Check if a string contains a palindromic sub-string of even length
- Lexicographically next string
- Lexicographically middle string
- Lexicographically n-th permutation of a string
- Lexicographically largest sub-sequence of the given string
- Lexicographically smallest string after M operations
- Find n-th lexicographically permutation of a string | Set 2
- Lexicographically largest string possible in one swap
- Lexicographically next greater string using same character set