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