Python Solution


  • 0
    S
    class Solution(object):
        def frequencySort(self, s):
            """
            :type s: str
            :rtype: str
            """
            num_to_freq = {}
            for ch in s:
                num_to_freq[ch] = num_to_freq.get(ch, 0) + 1
            
            freq_to_num = {}
            noRepetition = set()
            for ch in s:
                if ch not in noRepetition:
                    val = num_to_freq[ch]
                    if val in freq_to_num:
                        freq_to_num[val] += ch * val
                    else:
                        freq_to_num[val] = ch * val
                noRepetition.add(ch)
            allKeys = sorted(freq_to_num.keys(), reverse =True)
            retStr = ""
            for item in allKeys:
                retStr += freq_to_num[item]
            return retStr

Log in to reply
 

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