Skip to content

Commit ac9d602

Browse files
committed
completed challenge to create anagram list from an array of strings
1 parent 7934887 commit ac9d602

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

anagram_list.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

0 commit comments

Comments
 (0)