File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string[] } strs
3+ * @return {string[][] }
4+ */
5+
6+ // ๋ฌธ์ ๋ ํ์์ผ๋ ์๊ฐ ๋ณต์ก๋ ์ธก๋ฉด์์ ํจ์จ์ด ๋๋ฌด ๋จ์ด์ง๋ ํ์ด ๋ฐฉ๋ฒ....
7+ var groupAnagrams = function ( strs ) {
8+ let outputArr = [ ] ;
9+ let countArr = [ ] ;
10+
11+ const A_ASCII = 'a' . charCodeAt ( 0 ) ;
12+ const Z_ASCII = 'z' . charCodeAt ( 0 ) ;
13+
14+ let charCounts = Z_ASCII - A_ASCII + 1 ;
15+ let charCountArr = new Array ( charCounts ) . fill ( 0 ) ; //์ธ๋ฑ์ค๊ฐ ์ํ๋ฒณ์ ๋ํ๋.
16+
17+ for ( str of strs ) {
18+ let strCountString = getStrCountString ( str ) ;
19+
20+ let hasSameCountIndex = countArr . findIndex ( ( item ) => item === strCountString ) ;
21+
22+ if ( hasSameCountIndex !== - 1 ) {
23+ outputArr [ hasSameCountIndex ] . push ( str ) ;
24+ } else {
25+ countArr . push ( strCountString ) ;
26+
27+ outputArr . push ( [ str ] ) ;
28+ }
29+ }
30+
31+ return outputArr ;
32+
33+ function getStrCountString ( str ) {
34+ let tempArr = [ ...charCountArr ] ;
35+
36+ for ( char of str ) {
37+ let charAscii = char . charCodeAt ( 0 ) ;
38+ let charIndex = charAscii - A_ASCII ;
39+ tempArr [ charIndex ] += 1 ;
40+ }
41+ return tempArr . join ( ',' ) ;
42+ }
43+ } ;
44+
45+
You canโt perform that action at this time.
0 commit comments