Turtle and Good Pairs
Contest: Codeforces Round 968 (Div. 2)
Difficulty rating: 1200
Tags: constructive algorithms, greedy, sortings, strings

Turtle gives you a string $$$s$$$, consisting of lowercase Latin letters.

Turtle considers a pair of integers $$$(i, j)$$$ ($$$1 \le i < j \le n$$$) to be a pleasant pair if and only if there exists an integer $$$k$$$ such that $$$i \le k < j$$$ and both of the following two conditions hold:

- $$$s_k \ne s_{k + 1}$$$;
- $$$s_k \ne s_i$$$ or $$$s_{k + 1} \ne s_j$$$.

Besides, Turtle considers a pair of integers $$$(i, j)$$$ ($$$1 \le i < j \le n$$$) to be a good pair if and only if $$$s_i = s_j$$$ or $$$(i, j)$$$ is a pleasant pair.

Turtle wants to reorder the string $$$s$$$ so that the number of good pairs is maximized. Please help him!

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the string.

The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output the string $$$s$$$ after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them.

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

Note:
In the first test case, $$$(1, 3)$$$ is a good pair in the reordered string. It can be seen that we can't reorder the string so that the number of good pairs is greater than $$$1$$$. bac and cab can also be the answer.

In the second test case, $$$(1, 2)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(2, 4)$$$, $$$(2, 5)$$$, $$$(3, 5)$$$ are good pairs in the reordered string. efddd can also be the answer.

Example input:
5
3
abc
5
edddf
6
turtle
8
pppppppp
10
codeforces

Example output:
acb
ddedf
urtlet
pppppppp
codeforces

Editorial:
Partition the string $$$s$$$ into several maximal contiguous segments of identical characters, denoted as $$$[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$$$. For example, the string "aabccc" can be divided into $$$[1, 2], [3, 3], [4, 6]$$$.
We can observe that a pair $$$(i, j)$$$ is considered a "good pair" if and only if the segments containing $$$i$$$ and $$$j$$$ are non-adjacent.
Let $$$a_i = r_i - l_i + 1$$$. Then, the number of good pairs is $$$\frac{n(n - 1)}{2} - \sum\limits_{i = 1}^{m - 1} a_i \cdot a_{i + 1}$$$. Hence, the task reduces to minimizing $$$\sum\limits_{i = 1}^{m - 1} a_i \cdot a_{i + 1}$$$.
If $$$s$$$ consists of only one character, simply output the original string. Otherwise, for $$$m \ge 2$$$, it can be inductively proven that $$$\sum\limits_{i = 1}^{m - 1} a_i \cdot a_{i + 1} \ge n - 1$$$, and this minimum value of $$$n - 1$$$ can be achieved.
As for the construction, let the maximum frequency of any character be $$$x$$$, and the second-highest frequency be $$$y$$$. Begin by placing $$$x - y$$$ instances of the most frequent character at the start. Then, there are $$$y$$$ remaining instances of both the most frequent and the second most frequent characters. Next, sort all characters by frequency in descending order and alternate between them when filling the positions (ignoring any character once it's fully used). This guarantees that, except for the initial segment of length $$$\ge 1$$$, all other segments have a length of exactly $$$1$$$.
Time complexity: $$$O(n + |\Sigma| \log |\Sigma|)$$$ per test case, where $$$|\Sigma| = 26$$$.