|
| 1 | +# Little Ashish got a lot of strings as his birthday gift. He does not mind getting so many strings for |
| 2 | +# free; in fact, he loves them. But, on noticing all the strings he received as a gift, Little Ashish, who's |
| 3 | +# also a snob and a bit OCD kind of a guy, realizes that he does not like the way in which the strings are |
| 4 | +# arranged. |
| 5 | +# |
| 6 | +# He likes his strings sorted, in a different kind of a way. He wants his strings to be sorted based on the |
| 7 | +# count of characters present in the string. For instance, if the string is: "aaabbc", then the desired |
| 8 | +# string would be: cbbaaa. In case where the count of two characters is same, then the lexicographically |
| 9 | +# smaller one will be printed first. For instance: "aabbcc" then, the output will be: "aabbcc". |
| 10 | +# |
| 11 | +# Input: |
| 12 | +# First line of input contains number of test cases T. Each test case contains a single string S. |
| 13 | +# |
| 14 | +# Output: |
| 15 | +# For each test cases print the sorted string. |
| 16 | +# |
| 17 | +# Constraints: |
| 18 | +# 1<=T<=100 |
| 19 | +# 1<=|S|<=100 |
| 20 | +# |
| 21 | +# Note: |
| 22 | +# String contains only lowercase characters ['a' to 'z']. |
| 23 | +# |
| 24 | +# SAMPLE INPUT |
| 25 | +# 3 |
| 26 | +# aabbccdd |
| 27 | +# aabcc |
| 28 | +# hackerearth |
| 29 | +# |
| 30 | +# SAMPLE OUTPUT |
| 31 | +# aabbccdd |
| 32 | +# baacc |
| 33 | +# cktaaeehhrr |
| 34 | + |
| 35 | +from collections import Counter |
| 36 | + |
| 37 | +for _ in range(int(input())): |
| 38 | + string = Counter(input()) |
| 39 | + sorted_array = sorted(string.items(), key=lambda x: (x[1], x[0])) |
| 40 | + result = '' |
| 41 | + for items in sorted_array: |
| 42 | + result += items[0] * items[1] |
| 43 | + print(result) |
0 commit comments