File tree Expand file tree Collapse file tree 1 file changed +9
-11
lines changed
Expand file tree Collapse file tree 1 file changed +9
-11
lines changed Original file line number Diff line number Diff line change 44 * @return {boolean }
55 */
66var isAnagram = function ( s , t ) {
7- //1. s와 t를 Array로 만든다.
8- const sArray = s . split ( "" ) ;
9- const tArray = t . split ( "" ) ;
7+ const counter = new Array ( 26 ) . fill ( 0 ) ;
108
11- //2. sArray와 tArray의 sort를 같게 만든다.
12- const sortedSArray = sArray . sort ( ) ;
13- const sortedTArray = tArray . sort ( ) ;
9+ //edge case: s와 t의 길이가 다른 경우
10+ if ( s . length !== t . length ) return false ;
1411
15- //3. sArray와 tArray가 같은지 판별한다.
16- const result = JSON . stringify ( sortedSArray ) === JSON . stringify ( sortedTArray ) ;
17-
18- //4. 같으면 true를, 다르면 false를 반환한다.
19- return result ;
12+ for ( let i = 0 ; i < s . length ; i ++ ) {
13+ counter [ s . charCodeAt ( i ) - 97 ] ++ ;
14+ counter [ t . charCodeAt ( i ) - 97 ] -- ;
15+ }
2016
17+ return counter . every ( count => count === 0 ) ;
2118} ;
19+
You can’t perform that action at this time.
0 commit comments