File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ strs์ ๋ฌธ์์ด๋ค์ ๋ฑ์ฅ๋น๋๋ฅผ ๋ฐํ์ผ๋ก ๊ณ์ฐํ ํน์ ๋ฌธ์์ด๋ก ์ ํํ๊ณ ๊ฐ์ ๊ฒ๋ผ๋ฆฌ ๋ฌถ๋๋ค.
3+
4+ ์๊ฐ๋ณต์ก๋ : O(N * K) - N์ strs์ ๊ฐ์, K๋ strs์ ๊ธธ์ด. N ์ํ ์์ K ์ํ
5+ */
6+
7+ function groupAnagrams ( strs : string [ ] ) : string [ ] [ ] {
8+ const anagramMap = { }
9+ const result = strs . reduce ( ( acc , cur ) => {
10+ const charArray = new Array ( 26 ) . fill ( 0 )
11+
12+ for ( let char of cur ) {
13+ const charIdx = char . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 )
14+ charArray [ charIdx ] += 1
15+ }
16+
17+ const sortedValue = charArray . join ( '#' )
18+
19+ const targetIdx = anagramMap [ sortedValue ]
20+ if ( targetIdx !== undefined ) {
21+ acc [ targetIdx ] . push ( cur )
22+ } else {
23+ anagramMap [ sortedValue ] = acc . length
24+ acc . push ( [ cur ] )
25+ }
26+ return acc
27+ } , [ ] )
28+
29+ return result
30+ }
You canโt perform that action at this time.
0 commit comments