Created
September 3, 2025 03:18
-
-
Save AbeEstrada/4c78f6559b681386752ca5cf2180d9b8 to your computer and use it in GitHub Desktop.
Exercise: Hash Tables Ransom Note
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
function checkMagazine(magazine, note) { | |
// Create a frequency map of words available in the magazine | |
const availableWords = new Map(); | |
for (const word of magazine) { | |
const currentCount = availableWords.get(word) ?? 0; | |
availableWords.set(word, currentCount + 1); | |
} | |
// Check if we can form the note using available words | |
for (const requiredWord of note) { | |
const remainingCount = availableWords.get(requiredWord) ?? 0; | |
if (remainingCount === 0) { | |
console.log("No"); | |
return; | |
} | |
availableWords.set(requiredWord, remainingCount - 1); | |
} | |
console.log("Yes"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment