string t= abacaba the upsides of the prefix work in positions 1,2,… ,7 are equivalent to [0,0,1,0,1,2,3]. Let f(t) be equivalent to the greatest worth of the prefix capacity of string t over the entirety of its positions. For instance, f(abacaba)=3.
Correct answer will be upvoted else downvoted.
for string t= abacaba the upsides of the prefix work in positions 1,2,… ,7 are equivalent to [0,0,1,0,1,2,3].
Let f(t) be equivalent to the greatest worth of the prefix capacity of string t over the entirety of its positions. For instance, f(abacaba)=3.
You are given a string s. Reorder its characters self-assertively to get a string t (the number of events of any character in strings s and t should be equivalent). The worth of f(t) should be limited. Out of all choices to limit f(t), pick the one where string t is the lexicographically littlest.
Input
Each test contains various experiments. The principal line contains the number of experiments t (1≤t≤105). Portrayal of the experiments follows.
The main line of each experiment contains string s (1≤|s|≤105) comprising of lowercase English letters.
It is ensured that the amount of lengths of s over all experiments doesn't surpass 105.
Output
For each experiment print a solitary string t.
The multisets of letters in strings s and t should be equivalent. The worth of f(t), the limit of prefix capacities in string t, should be pretty much as little as could be expected. String t should be the lexicographically littlest string out of all strings fulfilling the past conditions.
Step by step
Solved in 3 steps with 1 images