Solution


  • 0

    Approach #1 :(Dictionary)

    Traverse string s and store each letter and their number of occurrence in a dictionary. Then sort the dictionary by values in descending order and print the letters.

    """

    import operator
    class Solution(object):
    def frequencySort(self, s):

        mydict = dict()
        for i in xrange(len(s)):
            mydict[s[i]] = mydict.get(s[i],0) + 1 
        
        sorted_list = sorted(mydict.items(), key=operator.itemgetter(1),reverse=True)
        
        retval = ""
        for w in sorted_list: 
            retval = retval + w[0]*w[1]
        
        return retval
    

    """


Log in to reply
 

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