Skip to content

Latest commit

 

History

History
20 lines (18 loc) · 448 Bytes

451.md

File metadata and controls

20 lines (18 loc) · 448 Bytes

451. Sort Characters By Frequency

Solution 1 (time O(n*log(n)), space O(1))

class Solution(object):
    def frequencySort(self, s):
        """
        :type s: str
        :rtype: str
        """
        d = collections.defaultdict(int)
        for c in s:
            d[c] += 1
        d = sorted(d.items(), key=lambda x:x[1], reverse=True)
        ans = ""
        for k, v in d:
            ans += k * v
        return ans