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.
- Print all permutations in sorted (lexicographic) order
- Longest palindromic String formed using concatenation of given strings in any order
- Print all palindrome permutations of a string
- Print all permutations of a string in Java
- Print all palindromic partitions of a string
- Given a string, print all possible palindromic partitions
- Write a program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates
- Find the count of substrings in alphabetic order
- Java Program to print distinct permutations of a string
- Print first n distinct permutations of string using itertools in Python
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Number of palindromic permutations | Set 1
- Print words of a string in reverse order
- Print a number as string of 'A' and 'B' in lexicographic order
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.