Ninja is trying to decipher a string 'S' that contains an out-of-order English representation of the digits 0 - 9.
Your task is to help Ninja to get back the digits in ascending order.
For example :
Given string βSβ = βtoowenβ, on the rearrangement of this string we get βonetwoβ which implies β12β.
The first line of input contains an integer 'T' representing the number of test cases.The 'T' test cases follow.
The first and the only line of each test case contains a single string 'S'.
Output Format :
For each test case, print a string of digits 0-9 in ascending order which we can get after rearrangement of the given string 'S'.
The output for each test case is printed in a separate line.
1 <= T <= 5
1 <= N <= 5000
βSβ is always valid.
Where βNβ is the length of the string βSβ.
Time limit: 1 sec
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
2
ffoiuver
oenenniowt
45
129
Test Case 1: We can rearrange βSβ as βfourfiveβ which implies β45β.
Test Case 2 : We can rearrange βSβ as βonetwonineβ which implies β129β.
2
tsirxee
szixeoneor
36
016
Some alphabets are unique to some digits.
Here, we are going to use an observation that some alphabets are unique to some digits as illustrated below:
And as the string is always valid, to find the occurrence of other digits we need to find the frequency of any single character from the digit's English word.
To find βHβ in three we just need to subtract the total βHβ frequency with the frequency of β8β because both contain βHβ.
Similarly, to find five we find the remaining βFβ after four, to find seven we need to find βVβ and for one and nine we need to calculate βOβ and βIβ respectively.
O(N), where βNβ is the length of the string βSβ.
We are iterating all characters one by one and incrementing the frequency.
O(1).
The total number of distinct characters is 26 (constant), and we are storing their frequencies, so, space complexity is O(1).