forked from drjkuo/leetcode-javascript-python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path242.ValidAnagram.js
More file actions
47 lines (36 loc) · 763 Bytes
/
242.ValidAnagram.js
File metadata and controls
47 lines (36 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
// slower solution
// Runtime: 142 ms; 38%
var isAnagram = function(s, t) {
let l1 = s.length;
let l2 = t.length;
if (l1 !== l2) return false;
s = s.split("").sort().join("");
t = t.split("").sort().join("");
return (s === t);
};
// hashmap
var isAnagram = function(s, t) {
var l1 = s.length;
var l2 = t.length;
var h1 = {};
if (l1 !== l2) return false;
for (var i=0; i<l1; i++) {
h1[s[i]] = h1[s[i]] || 0;
h1[s[i]]++;
}
for (i=0; i<l2; i++) {
h1[t[i]] = h1[t[i]] || 0;
h1[t[i]]--;
}
for (i in h1) {
if (h1[i] !== 0) {
return false;
}
}
return true;
};