File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ๋ฌธ์์ด ๋ฐฐ์ด strs๊ฐ ์ฃผ์ด์ก์ ๋, ์ ๋๊ทธ๋จ๋ผ๋ฆฌ ๊ทธ๋ฃนํํ๋ ๋ฌธ์
3
+ * ์ ๋๊ทธ๋จ: ๊ฐ์ ๋ฌธ์๋ฅผ ์ฌ๋ฐฐ์ดํ์ฌ ๋ง๋ค ์ ์๋ ๋จ์ด๋ค
4
+ *
5
+ * ์ ๊ทผ ๋ฐฉ๋ฒ: ๊ฐ ๋ฌธ์ ์ถํ๋น๋๋ฅผ ์นด์ดํ
ํ๋ ๋ฐฉ์
6
+ * 1. ๊ฐ ๋ฌธ์์ด์ ์ํ๋ฒณ ๊ฐ์๋ก ๋ณํํ์ฌ ํค๋ฅผ ์์ฑ
7
+ * 2. Map์ ์ฌ์ฉํ์ฌ ํค๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๊ทธ๋ฃนํ
8
+ * 3. Map์ ๊ฐ๋ค์ ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ๋ฐํ
9
+ *
10
+ * ์๊ฐ๋ณต์ก๋: O(n * k) (n: ๋ฌธ์์ด ๊ฐ์, k: ๋ฌธ์์ด ๊ธธ์ด) -> ๋จ์ ์นด์ดํ
11
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n * k) -> ๋ชจ๋ ๋ฌธ์์ด์ ์ ์ฅํด์ผ ํ๋ฏ๋ก
12
+ */
13
+
14
+ /**
15
+ * @param {string[] } strs
16
+ * @return {string[][] }
17
+ */
18
+ var groupAnagrams = function ( strs ) {
19
+ const map = new Map ( ) ;
20
+
21
+ for ( let str of strs ) {
22
+ const count = new Array ( 26 ) . fill ( 0 ) ; // ์ํ๋ฒณ ๊ฐ์ ์ด๊ธฐํ
23
+ for ( let i = 0 ; i < str . length ; i ++ ) {
24
+ const index = str . charCodeAt ( i ) - 'a' . charCodeAt ( 0 ) ; // ์ํ๋ฒณ ์ธ๋ฑ์ค ๊ณ์ฐ
25
+ count [ index ] ++ ; // ํด๋น ์ํ๋ฒณ ๊ฐ์ ์ฆ๊ฐ
26
+ }
27
+ const key = count . join ( '#' ) ; // ์ํ๋ฒณ ๊ฐ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ํค ์์ฑ
28
+ if ( ! map . has ( key ) ) {
29
+ map . set ( key , [ ] ) ; // ํค๊ฐ ์์ผ๋ฉด ์๋ก์ด ๋ฐฐ์ด ์์ฑ
30
+ }
31
+ map . get ( key ) . push ( str ) ; // ํด๋น ํค์ ๋ฌธ์์ด ์ถ๊ฐ
32
+ }
33
+
34
+ return Array . from ( map . values ( ) ) ; // Map์ ๊ฐ๋ค์ ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ๋ฐํ
35
+ } ;
You canโt perform that action at this time.
0 commit comments