-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode-242-ValidAnagram.js
95 lines (83 loc) · 2.91 KB
/
leetcode-242-ValidAnagram.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// https://leetcode.com/problems/valid-anagram/
// p: str & str
// r: boolean
// e:
// Example 1:
// Input: s = "anagram", t = "nagaram"
// Output: true
// Example 2:
// Input: s = "rat", t = "car"
// Output: false
// O(n) O(n)
var isAnagram = function (s, t) {
const map = {};
for (let c of s) {
map[c] = (map[c] || 0) + 1;
}
for (let c of t) {
if (!(c in map)) return false;
map[c]--;
}
for (let k in map) {
if (map[k]) return false;
}
return true;
};
// Runtime: 111 ms, faster than 52.78% of JavaScript online submissions for Valid Anagram.
// Memory Usage: 46.8 MB, less than 31.27% of JavaScript online submissions for Valid Anagram.
// O(nlog(n)) O(n)
var isAnagram = function (s, t) {
if (s.length !== t.length) return false;
s = s.split("").sort();
t = t.split("").sort();
for (let i = 0; i < s.length; i++) {
if (s[i] !== t[i]) return false;
}
return true;
};
// // loop & hash map
// // Runtime: 115 ms, faster than 47.28% of JavaScript online submissions for Valid Anagram.
// // Memory Usage: 43.9 MB, less than 60.32% of JavaScript online submissions for Valid Anagram.
// var isAnagram = function (s, t) {
// if (s.length !== t.length) return false;
// // create maps:
// const mapS = {};
// const mapT = {};
// // loop through both strings and count chars:
// for (let i = 0; i < s.length; ++i) {
// mapS[s[i]] = (mapS[s[i]] || 0) + 1;
// mapT[t[i]] = (mapT[t[i]] || 0) + 1;
// }
// // loops through a map and compare keys of both maps
// for (const key in mapS) {
// if (mapS[key] !== mapT[key]) return false;
// }
// return true;
// };
// loop & hash map
// // Runtime: 148 ms, faster than 18.23% of JavaScript online submissions for Valid Anagram.
// // Memory Usage: 43.2 MB, less than 76.92% of JavaScript online submissions for Valid Anagram.
// var isAnagram = function (s, t) {
// if (s.length !== t.length) return false;
// // create maps:
// const mapS = {};
// const mapT = {};
// // loop through both strings and count chars:
// for (let i = 0; i < s.length; ++i) {
// mapS[s[i]] = (mapS[s[i]] || 0) + 1;
// mapT[t[i]] = (mapT[t[i]] || 0) + 1;
// }
// console.log(mapS, mapT);
// // loops through a string again and compare both maps ////////////// need optimization so it only loops thru chars in mapS or mapT
// for (let char of s) {
// if (mapS[char] !== mapT[char]) return false;
// }
// return true;
// };
// Runtime: 211 ms, faster than 6.34% of JavaScript online submissions for Valid Anagram.
// Memory Usage: 49.5 MB, less than 8.59% of JavaScript online submissions for Valid Anagram.
// var isAnagram = function (s, t) {
// console.log(s.split("").sort().join("") === t.split("").sort().join(""));
// return s.split("").sort().join("") === t.split("").sort().join("");
// };
isAnagram("anagram", "nagaram");