AmazonOA #44
Get Encoded Name (Lex-Smallest Palindrome)
EasyJudge-ready
You are given a string letters that contains only lowercase letters and is itself a palindrome. Rearrange its characters and, among all rearrangements that are still palindromes, output the lexicographically smallest one.
Input
A single line containing the string letters. 1≤|letters|≤3×10^7; it contains only lowercase letters a-z, and letters is a palindrome.
Output
Output one line: the lexicographically smallest palindromic rearrangement.
Example 1
Input:
yxxy
Output:
xyyx
Example 2
Input:
cbabc
Output:
bcacb
Example 3
Input:
baab
Output:
abba
Example notes
Example 1: the characters of yxxy are two x and two y; the palindromic rearrangements are xyyx and yxxy, and the smaller one is xyyx. Example 2: in cbabc, a appears once and can only go in the center; the first half takes b and c, giving bcacb. Example 3: baab is rearranged into abba.
Editorial
Loading solution…