DSA Trainer
← Back to Foundations
Hash Maps/Bonus: spot the bug
Quick win · counts toward your unitFrequency MapAI-assisted round

Build a note from magazine letters

Given a note and a magazine string, return true if the note can be built using the magazine's letters. Each magazine letter can be used at most once.

AIThe assistant proposed this solution

I put the magazine's letters in a set for O(1) lookups, then check that every character the note needs is available. Using a set keeps it fast and simple.

1function canConstruct(note, magazine) {
2 const available = new Set(magazine);
3 for (const ch of note) {
4 if (!available.has(ch)) return false;
5 }
6 return true;
7}

Before you accept this, make the call. What's your verdict?