DEV Community

Achilonu Chinwendu Faustina
Achilonu Chinwendu Faustina

Posted on

DAY 5 CODE CHALLENGE

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

Image description

Top comments (0)