-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathsangbeenmoon.py
More file actions
35 lines (24 loc) · 847 Bytes
/
sangbeenmoon.py
File metadata and controls
35 lines (24 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# TC : O(m^2 * n) m : len(strs), n : len(strs[0])
# SC : O(m * n)
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
sorted_strs = []
for str in strs:
sorted_strs.append(sorted(str))
answer = []
i = 0
visited = [False] * 10001
while i < len(strs):
if visited[i]:
i = i + 1
continue
sub_answer = []
target = sorted_strs[i]
sub_answer.append(strs[i])
for j in range(i+1, len(strs)):
if sorted_strs[j] == target:
visited[j] = True
sub_answer.append(strs[j])
i = i + 1
answer.append(sub_answer)
return answer