#K85277. Rearranging Product Name into a Palindrome
Rearranging Product Name into a Palindrome
Rearranging Product Name into a Palindrome
You are given a list of product names. For each product name, determine whether its characters can be rearranged to form a palindrome. A palindrome is a string which reads the same backward as forward. If it is possible, you should output YES
followed by one valid palindromic permutation of that product name. Otherwise, output NO
.
For example, given the product name "AABB", one possible palindrome is "ABBA" (another acceptable answer is "BAAB"). However, the name "CAR" cannot be rearranged to form a palindrome. Note that if a product name is already a palindrome, simply output it along with YES
.
Hint: A string can be rearranged into a palindrome if and only if at most one character has an odd frequency.
inputFormat
The input is given from standard input and consists of multiple lines.
- The first line contains an integer n (1 ≤ n ≤ 105), the number of product names.
- The following n lines each contain a non-empty string representing a product name. The product name consists of uppercase English letters.
outputFormat
For each product name, print a line to standard output:
- If a palindrome permutation exists, output
YES
followed by a space and one valid palindromic permutation. - If no such permutation exists, output
NO
.
3
AABB
CAR
LEVEL
YES ABBA
NO
YES LEVEL
</p>