-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
51 lines (44 loc) · 1.36 KB
/
my_solution.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
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
const canConstruct = (ransomNote, magazine) => {
let ransomNoteMap = new Map(), magazineMap = new Map();
for (let i = 0; i < ransomNote.length; i++) {
ransomNoteMap.set(ransomNote[i], (ransomNoteMap.get(ransomNote[i]) || 0) + 1);
}
for (let i = 0; i < magazine.length; i++) {
magazineMap.set(magazine[i], (magazineMap.get(magazine[i]) || 0) + 1);
}
console.log("ransomNoteMap")
console.log(ransomNoteMap)
console.log("magazineMap")
console.log(magazineMap)
// for (let [key, value] of ransomNoteMap) {
// if (magazineMap.has(key)) {
// ransomNoteMap.set(key, value - 1);
// // && magazineMap.get(key) === value
// // return true;
// }
// console.log(ransomNoteMap)
// }
for (let i = 0; i < ransomNote.length; i++) {
if (magazineMap.has(ransomNote[i]) && magazineMap.get(ransomNote[i]) > 0) {
magazineMap.set(ransomNote[i], magazineMap.get(ransomNote[i]) - 1)
} else {
return false;
}
}
console.log("ransomNoteMap")
console.log(ransomNoteMap)
console.log("magazineMap")
console.log(magazineMap)
return true;
};
let x = null;
x = canConstruct("bg", "efjbdfbdgfjhhaiigfhbaejahgfbbgbjagbddfgdiaigdadhcfcj");
x = canConstruct("aa", "ab");
x = canConstruct("aa", "aab");
console.log("Result")
console.log(x)