solve the problem only by dict in Python


  • 0
    Q

    class Solution(object):
    def frequencySort(self, s):
    """
    :type s: str
    :rtype: str
    """
    s1=set(s)
    print(s1)
    g={}
    B=[]
    for i in s1:
    g[i]=0
    for i in s:
    g[i]=g[i]+1
    print(g)
    D=g.items()
    print(D)
    C=sorted(D,cmp=lambda x,y:cmp(x[1],y[1]),reverse=True)
    for i in C:
    B.append(str(i[0]*i[1]))
    return(''.join(B))


Log in to reply
 

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