-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathransom.js
48 lines (46 loc) · 1.25 KB
/
ransom.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
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function (ransomNote, magazine) {
let ransomArr = ransomNote.split('')
let magazineArr = magazine.split('')
let ransomCount = {}
let magCount = {}
for (const x of ransomArr) {
if (!ransomCount[x]) {
ransomCount[x] = 1
} else {
ransomCount[x]++
}
}
for (const x of magazineArr) {
if (!magCount[x]) {
magCount[x] = 1
} else {
magCount[x]++
}
}
let bool = false;
for (const x of ransomArr) {
if ((ransomCount[x] && magCount[x]) && ransomCount[x] <= magCount[x]) {
console.log(`${x} matched`)
bool = true
}
else {
return false
}
}
console.log(ransomCount, magCount)
return bool
};
var canConstructv2 = function (ransomNote, magazine) {
for(i=0;i<ransomNote.length;i++){
let bool = magazine.match(new RegExp(`${ransomNote[i]}`, 'g'))?.length >= ransomNote.match(new RegExp(`${ransomNote[i]}`, 'g'))?.length;
console.log(`${ransomNote[i]}`);
if(!bool) return false
}
return true
}
console.log(canConstructv2('ba', 'aab'))