SORT CHARACTERS BY FREQUENCY
A string s= tree is given to rearrange in decreasing order of frequency such that, the output will be "eetr" or "eert".
APPROACH
Check the frequency of the characters;
e = 2;
t = 1;
r = 1;
writing this in decreasing order of frequency;
output = eetr
or
output = eert
Here r and t are of the same frequency, therefore anyone can be written first.
PROCEDURE
I saved the frequencies in a hashtable.
I sorted them (frequencies) in decreasing order.
I rebuilt the strings based on frequencies.
OUTCOME
Top comments (0)