Skip to content

Commit 039cf39

Browse files
committed
add the solution for lettcode 242
1 parent 17b37e5 commit 039cf39

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ LeetCode
186186
|260|[Single Number III](https://leetcode.com/problems/single-number-iii/)| |Medium|
187187
|258|[Add Digits](https://leetcode.com/problems/add-digits/)| |Easy|
188188
|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|
190190
|241|[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)||Medium|
191191
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)||Medium|
192192
|239|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)| |Hard|
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
};

0 commit comments

Comments
 (0)