Skip to content

Commit a21373f

Browse files
committed
feat: group anagrams together
1 parent 0f4836b commit a21373f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

groupanagrams.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def groupAnagrams(self, strs):
3+
groups = {}
4+
for s in strs:
5+
s1 = ''.join(sorted(s))
6+
if not s1 in groups:
7+
groups[s1] = []
8+
groups[s1].append(s)
9+
return groups.values()
10+
11+
12+
if __name__ == '__main__':
13+
s = Solution()
14+
print(s.groupAnagrams(["abc", "bac", "def", "fed", "efd", "ghi"]))
15+
# => [['abc', 'bac'], ['def', 'fed', 'efd'], ['ghi']]
16+

0 commit comments

Comments
 (0)