Created
January 26, 2017 15:50
-
-
Save jasonwaters/8e48136a422d1d7e936af36ecb2f0ef5 to your computer and use it in GitHub Desktop.
magazine ransom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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