Print all palindrome permutations of a string - GeeksforGeeks
Given a string, we need to print all possible palindromes that can be generated using letters of that string.
Examples:
Input: str = "aabcb" Output: abcba bacba nput: str = "aabbcadad" Output: aabdcdbaa aadbcbdaa abadcdaba abdacadba adabcbada adbacabda baadcdaab badacadab bdaacaadb daabcbaad dabacabad dbaacaabd
We strongly recommend you to minimize your browser and try this yourself first.
Generation of palindrome can be done by following steps,
- First we need to check whether letters of string can make a palindrome or not, if not then return.
- After above checking we can make half part of first palindrome string (lexicographically smallest) by taking half frequency of each letter of the given string.
- Now traverse through all possible permutation of this half string and each time add reverse of this part at the end and add odd frequency character in mid between if string is of odd length, for making the palindrome.
Read full article from Print all palindrome permutations of a string - GeeksforGeeks
No comments:
Post a Comment