Sort Characters By Frequency

Problem Id: 451 Difficulty: Medium


Intuition

Solution


class Solution(object):
    def frequencySort(self, s):
        """
        :type s: str
        :rtype: str
        """
        counts = [[s.count(c), c] for c in set(s)]
        counts.sort(reverse=True)
        return ''.join([
            c * count for c, count in counts
        ])