iq.lab
Python starts when a code cell comes near or you run one
easyHash maps and sets target 15 min

Ransom note

You want to spell a message, note, with letters cut out of a magazine, given as the string magazine. A letter you cut out is gone after one use, so if the magazine has two e's, the note can use at most two e's. Return True if you can cut out a letter for every letter of note, and False if not.

Letters left over in the magazine are fine, and an empty note can always be spelled.

Example 1
Inputnote = "cab", magazine = "abacus"OutputTrue

The magazine has a c, an a and a b to cut out. The second a, the u and the s are left over.

Example 2
Inputnote = "tool", magazine = "lots"OutputFalse

The note needs two o's and the magazine has one.

Example 3
Inputnote = "", magazine = "abc"OutputTrue

An empty note needs no letters.

Constraints
  • 0 ≤ len(note), len(magazine) ≤ 105

  • Both strings hold only the lowercase letters a to z.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.