Given a string str of size n. The problem is to print all the palindromic permutations of str in alphabetic order if possible else print “-1”.
Input : str = "aabb" Output : abba baab Input : malayalam Output : aalmymlaa aamlylmaa alamymala almayamla amalylama amlayalma laamymaal lamayamal lmaayaaml maalylaam malayalam mlaayaalm
Approach: Following are the steps:
- If possible, find the Lexicographically first palindromic string using the characters of str else print “-1” and return.
- If lexicographically first palindromic string is obtained then print it.
- Now, find the next higher palindromic string using the same set of characters of str. Refer this post.
The only difference between the two posts is that in one digits are being arranged and in the other lowercase characters are being arranged.
- If next higher palindromic string is obtained then print it and goto step 3 else return.
Note that the string str is always manipulated so as to get the palindromic strings using the steps mentioned above.
aalmymlaa aamlylmaa alamymala almayamla amalylama amlayalma laamymaal lamayamal lmaayaaml maalylaam malayalam mlaayaalm
Time Complexity: O(m*n), where m is the number of palindromic permutations of str.
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.
- Find the count of substrings in alphabetic order
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Split numeric, alphabetic and special symbols from a String
- Find the largest Alphabetic character present in the string
- Print all permutations in sorted (lexicographic) order
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Write a program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates
- Print all lexicographical greater permutations of a given string
- Make palindromic string non-palindromic by rearranging its letters
- Given a string, print all possible palindromic partitions
- Print all possible palindromic string formed using any pair of given strings
- Number of palindromic permutations | Set 1
- Count of K-size substrings having palindromic permutations
- Print all palindrome permutations of a string
- Print all permutations of a string in Java
- Longest palindromic String formed using concatenation of given strings in any order
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Print all palindromic partitions of a string
- Print the longest palindromic prefix of a given 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 firstname.lastname@example.org. 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.