Skip to content

Instantly share code, notes, and snippets.

@AbeEstrada
Created September 3, 2025 03:18
Show Gist options
  • Save AbeEstrada/4c78f6559b681386752ca5cf2180d9b8 to your computer and use it in GitHub Desktop.
Save AbeEstrada/4c78f6559b681386752ca5cf2180d9b8 to your computer and use it in GitHub Desktop.
Exercise: Hash Tables Ransom Note
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