Ransom Note

Easy#383counting

You get a note and a magazine. Find out whether the note can be put together from letters cut out of the magazine, if each letter of the magazine can be used only once. Our example: the note "hello" and the magazine "shelfloor" — yes, and s, f, r and one o are left over.

input
ransomNote, magazine: strings.
returns
true if ransomNote can be made of letters cut out of magazine, each letter of magazine used at most once; otherwise false.
constraints
  • 0 ≤ ransomNote.length ≤ 2000
  • 1 ≤ magazine.length ≤ 2000
  • ransomNote, magazine: lowercase English letters
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} ransomNote
 * @param {string} magazine
 * @return {boolean}
 */
function canConstruct(ransomNote, magazine) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more hash map problems

Pattern “Hash map” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.