12559 - Characters Sort By Frequency With Specific Order   

Description

(modified by leetcode)

Given a string, sort it in decreasing order based on the frequency of characters and with following rule:

If the frequency of characters is the same, then sort it with Uppercase > Lowercase > Digit.

if the frequency of characters is the same and have the same type(uppercase or lowercase or digit), then sort uppercase and lowercase type with dictionary order, and sort digit type with increasing order.

There are few examples below:

Input:
"tree"

Output:
"eert"

Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. And 'r' is before 't' since it is dictionary order

 
Input:
"aAbb54"

Output:
"bbAa45"

Explanation:
'b' appears twice while 'a', 'A', '5' and '4' are appear once.
So 'b' must appear before both 'a','A','5' and '4'. And Since the order with different type is upercase > lowercase > digit, 
then 'A' must appear before 'a','5' and '4', and 'a' must appear before '5' and '4'.
Finally since '5' and '4' have same frequency then sort it by increasing order.

Note : you need to implement the sorting by yourself without using standard library!!!

Please raise you hand to notify TA when you get AC.

We will check all the student's code after the exam. If you violate the rule, we will find out !!!!!!!!!

Input

T

S1

...

Si

T: the number of string testcase ( 5 < T < 20)

Si : the given string  (1 < length(S) < 400)

Note : the input string only contain three type of character (uppercase alpha , lowercase alpha and digit)

Note : Testcase hint

Testcase 1: same type with distinct different frequency. ex: 122333, abbccc, ABBCCC 

Testcase 2: different type with distinct different frequency. ex: a22BBB

Testcase 3: same type with same frequency. ex: 321, cba, CBA

Testcase 4: differnt type with same frequency. ex: 012abcABC, ABC012abc

Testcase 5-8: random testcase with all possible combination

Output

Output the string is sorted by frequency with specific order. (each output string followed by a newline characters.)

Sample Input  Download

Sample Output  Download

Tags




Discuss