Problem 328
Sort Characters by Frequency
Medium · 20 pts · String, Hash Table, Sorting
Problem
Rearrange the characters of a string so that the most frequent characters come first. All copies of the same character must stay together. When two characters appear the same number of times, the one with the smaller character code comes first (digits before uppercase letters, and uppercase before lowercase). Print the rearranged string.
Input
A single string S of English letters and digits.
Output
Print the rearranged string.
Constraints
1 <= |S| <= 500000
Example input
tree
Example output
eert
Explanation
In 'tree' the letter e appears twice, while r and t appear once each. So 'ee' comes first, then r before t (r has the smaller code): 'eert'.