Can any one tell me why my hashmap Python solution TLE? Should be the same algorithm with the 2nd most viewed Java solution, Thanks!


  • 0
    V
        result={}
        max1=0
        min1=len(s)
        for i in xrange(len(s)):
            if s[i] in result:
                result[s[i]]+=1
                if result[s[i]]>max1:
                    max1=result[s[i]]
                if result[s[i]]<min1:
                    min1=result[s[i]]
            else:   
                result[s[i]]=1
                if max1<1:
                    max1=1
                if min1>1:
                    min1=1
        
        rlist=[]
        for i in xrange(max1):
            rlist.append([])
        
        
        for key in result.keys():
            rlist[result[key]-1].append(key)
    
        finalstring=''
        
        for i in xrange(len(rlist)-1,-1,-1):
            if len(rlist[i])!=0:
                for w in rlist[i]:
                    for j in xrange(i+1):
                        finalstring=finalstring+w
        
        return finalstring

Log in to reply
 

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