-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathransomeNote.js
More file actions
34 lines (33 loc) · 780 Bytes
/
ransomeNote.js
File metadata and controls
34 lines (33 loc) · 780 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
const ransomNote = (magazine,note) => {
let freq1 = {};
let freq2 = {};
magazine = magazine.split(' ');
note = note.split(' ');
for (let i = 0; i < magazine.length; i++){
if (freq1[magazine[i]]){
freq1[magazine[i]]++;
} else {
freq1[magazine[i]] = 1;
}
}
for (let j = 0; j < note.length ; j++){
if (freq2[note[j]]){
freq2[note[j]]++;
} else {
freq2[note[j]] = 1;
}
}
for (let key in freq2){
if (key in freq1){
if (freq1[key] < freq2[key]){
return false;
} else {
freq1[key]--;
}
} else {
return false;
}
}
return true;
};
ransomNote('give me one grand today night','give one grand today');