Python's version of solution by order of frequency


  • 0
    class Solution:
        def frequencySort(self, s):
            """
            :type s: str
            :rtype: str
            """
            self.s = s
            
            dictionary = {}
            for letter in s:
                dictionary[letter] = dictionary.get(letter, 0)+1
    
            new_list = sorted([(y,x) for x,y in dictionary.items()],reverse = True)
    
            new_str = ""
            for i in new_list:
                new_str += i[1]*i[0]
    
            return new_str
    

Log in to reply
 

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