Skip to content

Commit

Permalink
feat: [Week 05-2] solve group-anagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaedie committed Jan 7, 2025
1 parent 4ca1ba9 commit 8bc155f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions group-anagrams/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Solution:
1) hash map 에 sorted_word를 키로, 해당 sorted_word 에 해당하는 요소들을 밸류로 넣습니다.
Time: O(n^2 logn)= O(n) * O(nlogn)
Space: O(n)
"""


class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagram_words = defaultdict(list)
# dict = {sorted_word: [word, word]}

for i in range(len(strs)):
word = strs[i]
sorted_word = "".join(sorted(word))
anagram_words[sorted_word].append(word)

result = []
for arr in anagram_words.values():
result.append(arr)
return result

0 comments on commit 8bc155f

Please sign in to comment.