Skip to content

Commit 9d26168

Browse files
committed
[week 5] solve 2
1 parent 5994960 commit 9d26168

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Blind 75 - Group Anagrams
3+
LeetCode Problem: https://leetcode.com/problems/group-anagrams/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(N * K) (N์€ strs์˜ ๊ธธ์ด, K๋Š” strs ๋‚ด ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด)
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(N * K)
6+
ํ’€์ด : ํ•ด์‹œ๋งต์„ ์ด์šฉํ•œ ํ’€์ด
7+
ASCII ๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด ์ด๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด์‹œ๋งต์— ์ €์žฅํ•œ๋‹ค
8+
9+
"""
10+
11+
from typing import List
12+
from collections import defaultdict
13+
class Solution:
14+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
15+
anagrams = defaultdict(list)
16+
17+
for s in strs:
18+
count = [0] * 26
19+
for char in s:
20+
count[ord(char) - ord('a')] += 1
21+
anagrams[tuple(count)].append(s)
22+
23+
return list(anagrams.values())
24+

0 commit comments

Comments
ย (0)