This straightforward implementation problem focuses on the frequency of the characters in the input string. We need to sort the input string with decreasing frequency characters.
So, we first create a frequency map of the input string, then sort the map according to the freq.values()
.
Then finally iterate over the map and build the result string.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity for this problem is pretty interesting. Theoretically speaking, it’s since we sort our dictionary values. But if you read the constraints of the problem, you notice that we would have only lowercase and uppercase alphabets in the input. So, where . So, our N is atmost for all input sizes. Constant time! Yippee!
-
Memory
The memory usage is since we use a hashmap to store the frequencies of the input characters. But then again, since , this too is constant space!
— A