We can use a Counter to do this quickly, but this is .
class Solution:
def frequencySort(self, s: str) -> str:
c = Counter(s)
return ''.join(sorted(s, key=lambda x: c[x], reverse=True))
However we can also just sort the items themselves in the counter itself, which is always only sorting 26 characters at most, so its .
class Solution:
def frequencySort(self, s: str) -> str:
freq = Counter(s)
freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
return ''.join([key * val for key, val in freq])