Simple Python Solution in 70ms

  • 0
    class Solution(object):
        def frequencySort(self, s):
            charMap = {}
            res = []
            for char in s:
                if char in charMap:
                    charMap[char] += 1
                    charMap[char] = 1
            sortedS = sorted(charMap.items(), key=lambda x: x[1], reverse=True)
            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.