File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-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
+
7
+ /**
8
+ * μ λν¬ν κ°λ§ νμΈνλ€.
9
+ * 1. core: Map μ¬μ©νκ³ , λͺ¨λ stringμ ν λΉνλ€.
10
+ * 2. stringμ μνν΄μ ν λΉνλ€.
11
+ * 3. mapμ μμκ° μμΌλ©΄ μμΌλ©΄ + 1, μμΌλ©΄ 1
12
+ * 4. mapμ μνν΄μ μμκ° μλ€λ©΄ -1μ ν΄μ€λ€.
13
+ * 5. HashMapμ λ€μ νλ² μνν΄μ, 1μ΄μ κ°μ΄ μλ€λ©΄ μ λν¬ν κ°μ΄ μλκΈ° λλ¬Έμ false, HashMapμ λͺ¨λ κ°λ€μ΄ 0μ΄λ©΄ true
14
+
15
+ * 곡κ°λ³΅μ‘λ O(N)
16
+ * μκ°λ³΅μ‘λ O(N)
17
+ */
18
+
19
+
20
+ var isAnagram = function ( s , t ) {
21
+ const hashMap = new Map ( ) ;
22
+
23
+ // μμΈ μ²λ¦¬
24
+ if ( s . length !== t . length ) {
25
+ return false ;
26
+ }
27
+
28
+
29
+ for ( const string of s ) {
30
+ if ( ! hashMap . has ( string ) ) {
31
+ hashMap . set ( string , 1 )
32
+ } else {
33
+ hashMap . set ( string , hashMap . get ( string ) + 1 )
34
+ }
35
+ }
36
+
37
+ for ( const string of t ) {
38
+ if ( hashMap . has ( string ) ) {
39
+ hashMap . set ( string , hashMap . get ( string ) - 1 ) ;
40
+ }
41
+ }
42
+
43
+ // 0μ΄ μλ κ°μ΄ μλκ²½μ° - false
44
+ for ( const [ key , value ] of hashMap ) {
45
+ // early return
46
+ if ( value > 0 ) return false ;
47
+ }
48
+
49
+ return true ;
50
+ } ;
You canβt perform that action at this time.
0 commit comments