-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group_Anagrams
33 lines (29 loc) · 967 Bytes
/
Group_Anagrams
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
primes={'a':2,'b':3,'c':5,'d':7,'e':11,'f':13,'g':17,'h':19,'i':23,'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:]