#K60597. Frequency Sort
Frequency Sort
Frequency Sort
You are given a string S
consisting of lowercase letters. Your task is to determine and print the unique characters of the string sorted primarily by their frequency (in descending order) and then in alphabetical order for characters with the same frequency.
Formally, let \( f(c) \) be the frequency of character \( c \) in S
. You must output the characters sorted such that for any two distinct characters \( c_1 \) and \( c_2 \), either \( f(c_1) > f(c_2) \) or \( f(c_1) = f(c_2) \) and \( c_1 < c_2 \) (in lexicographical order).
For example, given the string "bbaa"
, both 'a'
and 'b'
occur twice. Sorting them alphabetically gives "ab"
.
You will be provided with multiple test cases.
inputFormat
The first line contains an integer T
representing the number of test cases.
Each of the following T
lines contains a non-empty string S
consisting of lowercase letters.
outputFormat
For each test case, print a line containing the unique characters of the string sorted by frequency (in descending order) and then alphabetically for ties.
## sample2
bbaa
abcabc
ab
abc
</p>