#K59532. Unique Palindrome Formation
Unique Palindrome Formation
Unique Palindrome Formation
You are given a string s. Your task is to determine whether it is possible to rearrange the characters of s into a palindrome. If it is possible, output "YES" and on the next line output the lexicographically smallest palindrome that can be formed using all the characters of s. Otherwise, output "NO".
A string is a palindrome if it reads the same forwards and backwards. In a palindrome of even length, every character must appear an even number of times. In a palindrome of odd length, exactly one character is allowed to appear an odd number of times while the rest must appear an even number of times. Formally, let \( f(c) \) be the frequency of character \( c \). Then the string can be rearranged into a palindrome if and only if \[ \sum_{c} [f(c) \bmod 2] \le 1. \]
If the condition is satisfied, construct the palindrome by taking half of the characters (in sorted order), then the unique middle character (if any), then the reverse of the first half.
inputFormat
The first line contains an integer T (\(1 \le T \le 100\)), the number of test cases.
Each of the following T lines contains a non-empty string s consisting of lowercase English letters.
Input is given through standard input.
outputFormat
For each test case, if a palindrome can be formed, print "YES" on one line and on the next line print the lexicographically smallest palindrome obtainable by rearranging the characters of s. Otherwise, print "NO".
Output the results for each test case in the same order as the input, using standard output.
## sample4
aabb
abcba
abccba
abcd
YES
abba
YES
abcba
YES
abccba
NO
</p>