[Q] [Medium]
Given an array of strings strs, group the anagrams together.
You can return the answer in any order.
- 일단 문제가 짧아서 좋았다 22..
- collections.defaultdict 함수 쓰면 아주 간단하게 해결되는데 참고한 문제 해결 방법이 독특해서 재밌었다.
# Answer
import collections
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = collections.defaultdict(list)
for word in strs:
anagrams[''.join(sorted(word))].append(word)
return list(anagrams.values())
'Algorithm > Leetcode' 카테고리의 다른 글
[Top Interview 150 - Hashmap] 128. Longest Consecutive Sequence (0) | 2024.10.13 |
---|---|
[Top Interview 150 - Hashmap] 219. Contains Duplicate II (1) | 2024.10.12 |
[Top Interview 150 - Hashmap] 242. Valid Anagram (0) | 2024.10.12 |
[Top Interview 150 - Hashmap] 290. Word Pattern (0) | 2024.10.12 |
[Top Interview 150 - Hashmap] 205. Isomorphic Strings (0) | 2024.10.12 |