File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ ํ์ด
3+ - ๋ ๋จ์ด๊ฐ anagram ๊ด๊ณ๋ผ๋ฉด ๊ฐ ๋จ์ด์ ์ํ๋ฒณ ์๋ฅผ ๊ณ์ฐํ ๊ฒฐ๊ณผ๊ฐ ๊ฐ์ ๊ฒ์
๋๋ค
4+ - get_key ํจ์๋ฅผ ์ด์ฉํด์ ๋จ์ด๋ฅผ ์ด๋ฃจ๋ ์ํ๋ฒณ ์์ ๋ฐ๋ผ ๊ณ ์ ํ key๋ฅผ ์์ฑํฉ๋๋ค
5+ - key๋ฅผ ๊ด๋ฆฌํ๋ ํด์๋งต์ธ key_map์ ์ด์ฉํ์ฌ ํ์ฌ ๋ฐ๋ผ๋ณด๊ณ ์๋ ๋จ์ด์ anagram์ธ ๋จ์ด๊ฐ ์๋์ง ํ์ธํฉ๋๋ค
6+
7+ Big O
8+ - N: ๋ฐฐ์ด strs์ ํฌ๊ธฐ
9+ - K: ๋ฐฐ์ด strs์ ์์ ์ค ๊ฐ์ฅ ๊ธธ์ด๊ฐ ๊ธด ๋ฌธ์์ด์ ๊ธธ์ด
10+
11+ - Time complexity: O(N * K)
12+ - ๋ฐฐ์ด strs๋ฅผ ์ํํฉ๋๋ค -> N
13+ - ๊ฐ ๋ฌธ์์ด๋ง๋ค ์ํ๋ฒณ์ ์๋ฅผ ์ธ๊ธฐ ์ํด ํ ๋ฒ ์ํํฉ๋๋ค -> K
14+
15+ - Space complexity: O(N)
16+ - ๋ฐฐ์ด strs์ ์์ ๋ชจ๋ ๊ณ ์ ํ key๋ฅผ ์ง๋๊ณ ์์ ์ ์์ต๋๋ค
17+ ์ด ๊ฒฝ์ฐ key_map์ ํฌ๊ธฐ๋ N์ ๋น๋กํ์ฌ ์ ํ์ ์ผ๋ก ์ฆ๊ฐํ ์ ์์ต๋๋ค -> N
18+ '''
19+
20+ class Solution :
21+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
22+ def get_key (s : str ) -> str :
23+ count = [0 ] * 26
24+ for char in s :
25+ count [ord (char ) - ord ('a' )] += 1
26+ res = ""
27+ for c in count :
28+ res += str (c ) + ","
29+ return res
30+
31+ idx = 0
32+ key_map = {}
33+ res = []
34+ for s in strs :
35+ key = get_key (s )
36+ if key not in key_map :
37+ key_map [key ] = idx
38+ idx += 1
39+ res .append ([])
40+ res [key_map [key ]].append (s )
41+
42+ return res
You canโt perform that action at this time.
0 commit comments