File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ def group_anagrams (words ):
2
+ anagram_list = []
3
+ letter_counts = {}
4
+ already_included = set ()
5
+ next_word = 1
6
+
7
+ for word in words :
8
+ letter_counts [word ] = {}
9
+
10
+ for letter in list (word ):
11
+ if letter in letter_counts [word ]:
12
+ letter_counts [word ][letter ] += 1
13
+ else :
14
+ letter_counts [word ][letter ] = 1
15
+
16
+ for word in letter_counts :
17
+ if word in already_included :
18
+ next_word += 1
19
+ continue
20
+
21
+ anagrams = {word }
22
+
23
+ for index in range (next_word , len (letter_counts ) - 1 ):
24
+ possible_anagram = list (letter_counts )[index ]
25
+
26
+ if letter_counts [word ] == letter_counts [possible_anagram ]:
27
+ anagrams .add (possible_anagram )
28
+ already_included .add (possible_anagram )
29
+
30
+ index += 1
31
+
32
+ anagram_list .append (anagrams )
33
+
34
+ return anagram_list
You can’t perform that action at this time.
0 commit comments