-
Notifications
You must be signed in to change notification settings - Fork 8
/
groupAnagram.py
34 lines (30 loc) · 1.06 KB
/
groupAnagram.py
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
34
def groupAnagrams(words):
anagrams = {}
for word in words:
sortedWord = ''.join(sorted(word))
if sortedWord in anagrams:
anagrams[sortedWord].append(word)
else:
anagrams[sortedWord] = [word]
return list(anagrams.values())
# def groupAnagrams(words):
# if len(words)==0:
# return []
# sortedWords = [''.join(sorted(word))for word in words]
# indices = [i for i in range(len(words))]
# indices.sort(key=lambda x: sortedWords[x])
# result = []
# currentAnagramGroup = []
# currentAnagram = sortedWords[indices[0]]
# for index in indices:
# word = words[index]
# sortedWord = sortedWords[index]
# if sortedWord == currentAnagram:
# currentAnagramGroup.append(word)
# continue
# result.append(currentAnagramGroup)
# currentAnagramGroup = [word]
# currentAnagram=sortedWord
# result.append(currentAnagramGroup)
# return result
print(groupAnagrams(["yo", "act", "flop", "tac", "foo", "cat", "oy", "olfp"]))