We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b909f60 commit 85a811cCopy full SHA for 85a811c
group-anagrams/seungriyou.py
@@ -0,0 +1,24 @@
1
+# https://leetcode.com/problems/group-anagrams/
2
+
3
+from typing import List
4
5
+class Solution:
6
+ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
7
+ """
8
+ [Approach]
9
+ - TC: O(n * klogk) (n = len(strs), k = max length of an element)
10
+ - SC: O(n * k)
11
12
+ [Complexity]
13
+ (1) strs의 원소들을 정렬해서 (2) hash table의 key 값으로 사용하면
14
+ 매 단계마다 O(1)에 anagram을 찾을 수 있다.
15
+ 이때, key 값으로는 list는 사용할 수 없고, string이나 tuple을 사용해야 한다.
16
17
+ from collections import defaultdict
18
19
+ anagrams = defaultdict(list)
20
21
+ for s in strs:
22
+ anagrams["".join(sorted(s))].append(s)
23
24
+ return list(anagrams.values())
0 commit comments