Skip to content

Instantly share code, notes, and snippets.

@jasonwaters
Created January 26, 2017 15:50
Show Gist options
  • Select an option

  • Save jasonwaters/8e48136a422d1d7e936af36ecb2f0ef5 to your computer and use it in GitHub Desktop.

Select an option

Save jasonwaters/8e48136a422d1d7e936af36ecb2f0ef5 to your computer and use it in GitHub Desktop.
magazine ransom
/*
Given the words in the magazine and the words in the ransom note, print Yes if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No.
https://www.hackerrank.com/challenges/ctci-ransom-note?h_r=internal-search
*/
function magazineRansom(magazine, note) {
magazine = magazine
.split(' ')
.reduce((map, word) => {
if (!map[word]) {
map[word] = 0;
}
map[word]++;
return map;
}, {});
note = note.split(' ');
for (let word of note) {
if (typeof magazine[word] === 'number' && magazine[word] > 0) {
magazine[word]--;
} else {
return "No";
}
}
return "Yes";
}
console.log(magazineRansom('give me one grand today night', 'give one grand today'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment