File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -186,7 +186,7 @@ LeetCode
186
186
| 260| [ Single Number III] ( https://leetcode.com/problems/single-number-iii/ ) | | Medium|
187
187
| 258| [ Add Digits] ( https://leetcode.com/problems/add-digits/ ) | | Easy|
188
188
| 257| [ Binary Tree Paths] ( https://leetcode.com/problems/binary-tree-paths/ ) | | Easy|
189
- | 242| [ Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) | | Easy|
189
+ | 242| [ Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) | [ js ] ( ./algorithms/validAnagram/validAnagram.js ) | Easy|
190
190
| 241| [ Different Ways to Add Parentheses] ( https://leetcode.com/problems/different-ways-to-add-parentheses/ ) || Medium|
191
191
| 240| [ Search a 2D Matrix II] ( https://leetcode.com/problems/search-a-2d-matrix-ii/ ) || Medium|
192
192
| 239| [ Sliding Window Maximum] ( https://leetcode.com/problems/sliding-window-maximum/ ) | | Hard|
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
+ const map1 = new Map ( ) ;
8
+ const map2 = new Map ( ) ;
9
+
10
+ function countNum ( str , map ) {
11
+ for ( let i = 0 ; i < str . length ; i ++ ) {
12
+ const current = str [ i ]
13
+ const value = map . get ( current ) ;
14
+ if ( value ) {
15
+ map . set ( current , value + 1 )
16
+ } else {
17
+ map . set ( current , 1 )
18
+ }
19
+ }
20
+ }
21
+
22
+ countNum ( s , map1 ) ;
23
+ countNum ( t , map2 ) ;
24
+
25
+ if ( map1 . size !== map2 . size || (
26
+ [ ...map1 . keys ( ) ] . sort ( ) . join ( "" ) !== [ ...map2 . keys ( ) ] . sort ( ) . join ( "" )
27
+ ) ) {
28
+ return false ;
29
+ }
30
+
31
+ for ( let [ key , value ] of map1 ) {
32
+ if ( value !== map2 . get ( key ) ) {
33
+ return false ;
34
+ }
35
+ }
36
+
37
+ return true ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments