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