Simple Python Solution in 70ms


  • 0
    K
    class Solution(object):
        def frequencySort(self, s):
            charMap = {}
            res = []
            for char in s:
                if char in charMap:
                    charMap[char] += 1
                else:
                    charMap[char] = 1
            sortedS = sorted(charMap.items(), key=lambda x: x[1], reverse=True)
            print(sortedS)
            for char, occurances in sortedS:
                res += char * occurances
            return "".join(res)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.