Given string str, the task is to print the characters in decreasing order of their frequency. If the frequency of two characters is the same then sort them in descending order alphabetically.
Input: str = “geeksforgeeks”
e – 4
s – 2
k – 2
g – 2
r – 1
o – 1
f – 1
Input: str = “bbcc”
c – 2
b – 2
- Use an unordered_map to store the frequencies of all the elements of the given string.
- Find the maximum frequency element from the map, print it with its frequency, and remove it from the map.
- Repeat the previous step while the map is not empty.
Below is the implementation of the above approach:
e - 4 s - 2 k - 2 g - 2 r - 1 o - 1 f - 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.