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
+ * [Idea]
3
+ * s์์ ๊ฐ ๋ฌธ์๊ฐ ๋ช๋ฒ ๋ฑ์ฅํ๋์ง ์ธ์ด์ค๋ค. (charCount)
4
+ * t์์ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ธ์ด์ฃผ๋ฉด์ charCount์ ๊ฐ์ ๊ฐ์์ํจ๋ค.
5
+ * ๋ฌธ์์ ๊ฐ์๊ฐ ๋ถ์กฑํ๊ฑฐ๋ ๋จ๋๋ค๋ฉด ์ ๋๊ทธ๋จ์ด ์๋๋ค.
6
+ *
7
+ * [Time Complexity]
8
+ * O(n + n + k) => O(n) (n: s์ t์ ๊ธธ์ด, k: ๋ ๋ฌธ์์ด์ ๋ฑ์ฅํ๋ ๊ณ ์ ๋ฌธ์ ์)
9
+ *
10
+ * [Space Complexity]
11
+ * O(n)
12
+ * ๋ฌธ์ ๋ฑ์ฅ ํ์๋ฅผ ์ธ์ด์ฃผ๋ charCount์ ํ์ํ ๊ณต๊ฐ
13
+ */
14
+ function isAnagram ( s : string , t : string ) : boolean {
15
+ const charCount : Record < string , number > = { } ;
16
+ for ( const char of s ) {
17
+ charCount [ char ] = ( charCount [ char ] ?? 0 ) + 1 ;
18
+ }
19
+
20
+ for ( const char of t ) {
21
+ if ( charCount [ char ] === undefined || charCount [ char ] === 0 ) {
22
+ return false ;
23
+ }
24
+ charCount [ char ] -- ;
25
+ }
26
+
27
+ return Object . values ( charCount ) . every ( ( count ) => count === 0 ) ;
28
+ }
You canโt perform that action at this time.
0 commit comments