letters of even length n made of lowercase English letters. Its positions split into mirrored pairs: position i faces position n - 1 - i.
Rearrange the letters (swapping any two letters as many times as you like) so that no mirrored pair holds the same letter twice. Among all arrangements that work, return the smallest one in dictionary order.
If no such arrangement exists, return the string "-1".letters="zaza" → 'aazz'
In aazz the mirrored pairs are (a, z) and (a, z), so nothing matches. No smaller string can be made from these letters.
letters="cbbaba" → 'aabcbb'
The sorted string aabbbc fails because the two middle letters are both b. Rearranging the right half into cbb gives aabcbb with pairs (a, b), (a, b), (b, c).
letters="kkkkkm" → '-1'
There are three pairs but five k letters, so some pair must get two k. The answer is "-1".
letters) ≤ 10⁵letters is evenletters consists of lowercase English lettersletters="zaza"
'aazz'
Run checks the first 3 tests in your browser. Submit checks all 44 tests on the server, hidden ones included.