Easy to Understand Solution using Python3 with Dictionary


  • 0
    K
    def frequencySort(self, s):
        dic = {}
        l = []
        for char in s:
            if char not in dic:
                dic[char] = 1
            else:
                dic[char] += 1
        print(dic)
        for key in sorted(dic, key=dic.get, reverse=True):
            print(key)
            for i in range(dic[key]):
                l.append(key)
        return("".join(l))

Log in to reply
 

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