Skip to content

Commit c54d553

Browse files
committed
Solved the group-anagrams problem. I know it's just a small simple problem, but its consequences are staggering
1 parent 1ecfca0 commit c54d553

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

String/group-anagrams.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def group_anagrams(self, words):
3+
if not words: return
4+
5+
dct = {}
6+
7+
for word in words:
8+
sort_word = ''.join(sorted(word))
9+
if sort_word not in dct:
10+
dct[sort_word] = [word]
11+
else:
12+
dct[sort_word].append(word)
13+
14+
return dct.values()
15+
16+
strs = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat']
17+
18+
s = Solution()
19+
print(s.group_anagrams(strs))

0 commit comments

Comments
 (0)