Ugly Pairs
Contest: Educational Codeforces Round 64 (Rated for Div. 2)
Difficulty rating: 1800
Tags: dfs and similar, greedy, implementation, sortings, strings

You are given a string, consisting of lowercase Latin letters.

A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $$$(1, 2)$$$ — "ab" and $$$(2, 3)$$$ — "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet.

Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same.

If there are multiple answers, print any of them.

You also have to answer $$$T$$$ separate queries.

Input format:
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries.

Each of the next $$$T$$$ lines contains string $$$s$$$ $$$(1 \le |s| \le 100)$$$ — the string for the next query. It is guaranteed that it contains only lowercase Latin letters.

Note that in hacks you have to set $$$T = 1$$$.

Output format:
Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th query.

If the answer for the $$$i$$$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same.

If there are multiple answers, print any of them.

Otherwise print "No answer" for that query.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example answer "bdac" is also correct.

The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok.

There are lots of valid answers for the third example.

Example input:
4
abcd
gg
codeforces
abaca

Example output:
cadb
gg
codfoerces
No answer

Editorial:
To be honest, the solution to this problem is easier to code than to prove.
Let's follow the next strategy. Write down all the letters of the string which have odd positions in alphabet ("aceg$$$\dots$$$") and even positions in alphabet ("bdfi$$$\dots$$$"). Sort both of these lists in non-decreasing order. The answer is either concatenation of the lists (odd + even or even + odd) or "No answer".
Now for the proof part. Let's establish that we don't care about equal letters and leave just a single copy of each letter of the string. Let's check some cases:
1. There is just a single letter. That's trivial.
2. There are two letters of the same parity. There is no incorrect arrangement for this.
3. There are two letters of different parity. If they differ by one then no answer exists. Otherwise any arrangement works.
4. There are three letters and they are consecutive in alphabet. No answer exists.
5. There are other types of three letters. Then the one of the different parity can put on the side (e.g. "acd" and "dac"). As the difference between at least one of these letters and that one isn't 1, that arrangement will be ok.
6. Finally, there are at least 4 letters. It means that the difference between either the smallest odd and the largest even or between the smallest even and the largest odd isn't 1.
The only thing you need to do is to implement the check function the most straightforward way possible and check both arrangements.
Overall complexity: $$$O(n \log n)$$$.