Given a string str, the task is to print the characters in decreasing order of their frequency. If the frequency of two characters is 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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Sorting element of an array by frequency in decreasing order
- Print the frequency of each character in Alphabetical order
- Print characters having even frequencies in order of occurrence
- Print characters having odd frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence
- Print common characters of two Strings in alphabetical order
- Print all distinct characters of a string in order (3 Methods)
- Print characters having prime frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using Binary Tree
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Python code to print common characters of two Strings in alphabetical order
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum characters to be replaced to make frequency of all characters same
- Program to check if all characters have even frequency
- Remove odd frequency characters from the string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.