Simple and Easy Python solution beats 97% using Hash


  • 0
    N
    class Solution(object):
        def frequencySort(self, s):
            """
            :type s: str
            :rtype: str
            """
            d = {}
            for c in s:
                if c in d:
                    d[c] += 1
                else:
                    d[c] = 1
            rd = {}
            for k, v in d.items():
                if v in rd:
                    rd[v].append(k)
                else:
                    rd[v] = [k]
            r = []
            for k in reversed(sorted(rd.keys())):
                for v in rd[k]:
                    r.append(v * k)
            return ''.join(r)
                
    

Log in to reply
 

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