English, asked by deepads501, 1 year ago

write a program to print the sorted list of all palindromes that can be constructed from the alphabets of the given palindrome

Answers

Answered by Shaizakincsem
0

Just break this problem in two parts: 1. find first smallest palindrome 2. Find next string of left part of string(left side of mid char or say s[:len(s)/2]) and create palindrome then as leftPart + midChar + leftPart[::-1]For first part, it would easy: 1. just find all different characters that is used in string 2. Find frequencies of each char in string, decrease frq by 1 for odd frq char(assuming there will be 0 or 1 char with odd frq). 3. And just make a left string out of these strings using half of their frqs and use them in increasing order. 4. now smallest palindrome would be leftString(find from above steps) + midChar(odd frq char) + leftString[::-1]

For second part you just have to find next lexicographical bigger string of left string that you found from above steps. and create palindrome out of it in same way.

You can take help from here to find next lexicographical bigger string. In C++ you can use next_permutation(s.begin(), s.end()) in algorithm where s is string for which you have to find next permutation.


Similar questions