Here is a basic code i wrote in python by creating my own hash function. I am not able to pass the last testcase.


  • 0
    S
    primes={'a':2,'b':3,'c':5,'d':7,'e':11,'f':13,'g':17,'h':19,'i':21,'j':29,'k':31,'l':37,'m':41,'n':43,'o':47,'p':53,'q':59,'r':61,'s':67,'t':71,'u':73,'v':79,'w':83,'x':89,'y':97,'z':101}
    def hash_value(str):
        result=1
        for i in str:
            result*=primes[i]
    
        return result
    class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        if(len(strs)==1):
            return [strs]
        
        dicti=dict()
        for i in strs:
            if(hash_value(i) in dicti):
                dicti[hash_value(i)].append(i)
            else:
                dicti[hash_value(i)]=[i]
        
        answer=[[]]
        for key,value in dicti.items():
            answer.append(value)
        
        temp=[]
        for i in range(1,len(answer)):
            temp=answer[i]
            temp.sort()
            answer[i]=temp
        return answer[1:]

  • 0
    S

    The mistake is that i have not used a prime number for 'i', change it to 23 and it works.


Log in to reply
 

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