You have been given a string ‘S’.
You must return the string ‘S’ sorted in decreasing order based on the frequency of characters.
If there are multiple answers, return any of them.
Let 'S'= “abAb”.
Here the characters ‘a’ and ‘A’ have frequency 1 and character ‘b’ has frequency ‘2’.
Therefore the sorted string is “bbAa”.
The first line contains a single integer ‘N’ denoting the length of the string ‘S’.
The next line contains the string ‘S’.
Output Format :
The only line contains "true" if the answer is correct otherwise "false".
Note :
You do not need to print anything. It has already been taken care of. Just implement the function.
6
abcAbc
true
The frequency of characters ‘a’ and ‘A’ are 1. The frequency of characters ‘b’ and ‘c’ are 2.
Therefore the sorted string is “bbccAa”.
7
gggjhhh
true
1 <= 'N' <= 10^5
String 'S' consists of both lowercase and uppercase alphabet characters.
Time Limit: 1sec
Can you solve this in O(N*Log(N)) time?
Will finding the frequency of each character helps here ?
We will sort string ‘s’ as follows:-
O(N*Log(N)), where ‘N’ denotes the length of the string.
As we are sorting the string which takes O(N * Log(N)). Hence, the total time Complexity is O(N * Log(N)).
O(N), where ‘N’ denotes the length of the string.
As we are storing characters in a set, which takes O(N) space.Hence, the total Space Complexity is O(N).