![]() |
VOOZH | about |
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.
Examples:
Input: str = "geeksforgeeks"
Output:
e - 4
s - 2
k - 2
g - 2
r - 1
o - 1
f - 1
Input: str = "bbcc"
Output:
c - 2
b - 2
Approach 1:
Below is the implementation of the above approach:
e - 4 s - 2 k - 2 g - 2 r - 1 o - 1 f - 1
Approach 2 : We will make an array arr of size one more than the size of given string length in which we will store List of characters whose frequency is equal to the index of arr and follow the below steps :
Implementation of above approach :
e-4 s-2 k-2 g-2 r-1 o-1 f-1
Time Complexity : O(n), n is the length of given string
Auxiliary Space : O(n)