Loading...
Loading...
In a college word game, each team gets a jumble of letters. The team must rewrite the jumble so that the letter used most often comes first, with all its copies together, then the next most frequent letter, and so on. When two letters appear the same number of times, the one that comes first in the alphabet goes first.
Print the rewritten string.
Example 1
Input
banana
Output
aaannb
a appears 3 times, n twice and b once, so the answer is aaannb.
Example 2
Input
tree
Output
eert
e appears twice. t and r appear once each, and r comes first in the alphabet: eert.
Try the problem first. Open one hint at a time.
There are only 26 letters. Count each one in an array of size 26.
Sort the 26 letters, not the whole string: higher count first, and for equal counts the smaller letter first.
Then print each letter as many times as it was counted.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor