File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ // ์๊ฐ ๋ณต์ก๋: O(n) - ๊ฐ ๋ฌธ์์ด์ ํ ๋ฒ์ฉ๋ง ์ํ
2+ // ๊ณต๊ฐ ๋ณต์ก๋: O(1) - ์ต๋ 26๊ฐ์ ์๋ฌธ์๋ง ์ ์ฅ (์์ ๊ณต๊ฐ)
3+
4+ /**
5+ * @param {string } s
6+ * @param {string } t
7+ * @return {boolean }
8+ */
9+ const isAnagram = ( s , t ) => {
10+ // ๊ธธ์ด ์ฒดํฌ๋ ํ์
11+ if ( s . length !== t . length ) {
12+ return false ;
13+ }
14+
15+ // ๊ฐ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ ์ฅํ ๊ฐ์ฒด
16+ const charCount = { } ;
17+
18+ // ์ฒซ ๋ฒ์งธ ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์ ๊ฐ์ ์นด์ดํธ
19+ for ( let char of s ) {
20+ charCount [ char ] = ( charCount [ char ] || 0 ) + 1 ;
21+ }
22+
23+ // ๋ ๋ฒ์งธ ๋ฌธ์์ด์ ๋ฌธ์๋ฅผ ํ๋์ฉ ๋นผ๋ฉด์ ํ์ธ
24+ for ( let char of t ) {
25+ if ( ! charCount [ char ] ) {
26+ // ์๋ ๋ฌธ์๊ฑฐ๋ ์ด๋ฏธ ๋ค ์ฌ์ฉํ ๊ฒฝ์ฐ
27+ return false ;
28+ }
29+ charCount [ char ] -- ;
30+ }
31+
32+ return true ;
33+ } ;
You canโt perform that action at this time.
0 commit comments