Skip to content

Commit 8bc155f

Browse files
committed
feat: [Week 05-2] solve group-anagrams
1 parent 4ca1ba9 commit 8bc155f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

group-anagrams/Chaedie.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Solution:
3+
1) hash map 에 sorted_word를 키로, 해당 sorted_word 에 해당하는 요소들을 밸류로 넣습니다.
4+
Time: O(n^2 logn)= O(n) * O(nlogn)
5+
Space: O(n)
6+
"""
7+
8+
9+
class Solution:
10+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
11+
anagram_words = defaultdict(list)
12+
# dict = {sorted_word: [word, word]}
13+
14+
for i in range(len(strs)):
15+
word = strs[i]
16+
sorted_word = "".join(sorted(word))
17+
anagram_words[sorted_word].append(word)
18+
19+
result = []
20+
for arr in anagram_words.values():
21+
result.append(arr)
22+
return result

0 commit comments

Comments
 (0)