Skip to content

Commit 6f7ed62

Browse files
committed
group anagrams solution
1 parent 4273f10 commit 6f7ed62

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

group-anagrams/devyejin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
from collections import Counter, defaultdict
3+
4+
# class Solution:
5+
# def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
6+
#
7+
# dict_anagrams = defaultdict(list)
8+
# for idx, word in enumerate(strs):
9+
# key = tuple(sorted(Counter(word).items()))
10+
# dict_anagrams[key].append(word)
11+
#
12+
# return list(dict_anagrams.values())
13+
class Solution:
14+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
15+
16+
dict_anagrams = defaultdict(list)
17+
for word in strs:
18+
key = "".join(sorted(word))
19+
dict_anagrams[key].append(word)
20+
21+
return list(dict_anagrams.values())

0 commit comments

Comments
 (0)