Skip to content
LC-0451 Medium LeetCode

451. Sort Characters By Frequency

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 74% Topics: Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

import collections


class Solution(object):
    def frequencySort(self, s):
        """
        :type s: str
        :rtype: str
        """
        freq = collections.defaultdict(int)
        for c in s:
            freq[c] += 1

        counts = [""] * (len(s)+1)
        for c in freq:
            counts[freq[c]] += c

        result = ""
        for count in reversed(xrange(len(counts)-1)):
            for c in counts[count]:
                result += c * count

        return result

Solution from kamyu104/LeetCode-Solutions · MIT