File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @description ๋ฌธ์์ ์ข
๋ฅ์ ๊ฐ์๊ฐ ์์ ํ ๊ฐ์ ์ ๋๊ทธ๋จ์ธ์ง ํ๋ณ
3+ * @param {string } s ๋ฌธ์์ด 1
4+ * @param {string } t ๋ฌธ์์ด 2
5+ * @returns {boolean } ์ ๋๊ทธ๋จ ์ฌ๋ถ
6+ */
7+ function isAnagram ( s : string , t : string ) : boolean {
8+ // ๋ฌธ์์ด๋ค์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ฉด ์ ๋๊ทธ๋จ ์๋
9+ if ( s . length !== t . length ) return false ;
10+
11+ // ์์ด ์๋ฌธ์ ๊ฐ์ "a"~"z" = 26๊ฐ
12+ const LOWER_ALPHABET_COUNT = 26 ;
13+ // ์ํ๋ฒณ ๋ฑ์ฅ ํ์๋ฅผ ์ ์ฅํ ๋ฐฐ์ด
14+ // index 0 -> 'a'
15+ // index 1 -> 'b'
16+ const count = new Array ( LOWER_ALPHABET_COUNT ) . fill ( 0 ) ;
17+ // a์ ์์คํค์ฝ๋
18+ const BASE = "a" . charCodeAt ( 0 ) ;
19+
20+ for ( let i = 0 ; i < s . length ; i ++ ) {
21+ count [ s . charCodeAt ( i ) - BASE ] ++ ;
22+ count [ t . charCodeAt ( i ) - BASE ] -- ;
23+ }
24+
25+ // ๋ชจ๋ ๋ฌธ์์ ๋ฑ์ฅ ํ์๊ฐ 0์ด๋ฉด
26+ // s์ t์ ๋ฌธ์ ์ข
๋ฅ์ ๊ฐ์๊ฐ ์์ ํ ๋์ผ
27+ return count . every ( ( value ) => value === 0 ) ;
28+ }
You canโt perform that action at this time.
0 commit comments