Input: "tree"
Output: "eert"
Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
I tried something like this :
class Solution(object):
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
has = dict()
l = list()
for c in s:
if c not in has:
has[c] = 1
else:
has[c] += 1
for k in sorted(has,key = has.get, reverse = True):
for i in range(has[k]):
l.extend(k)
return ("".join(l))
but its O(n * m) n = length of string, m = maximum occurrence of a character
How can i improve this to order of n?