Skip to content

Ransom Note From a Magazine

Easy#frequency-count#hash-map~10 min

The mission

Problem

A sign maker cuts letters out of a spare banner to spell a new message. Given two lowercase strings note and banner, return True if note can be spelled using letters from banner, where each letter of banner can be used at most once. Both strings have up to 100,000 letters, so avoid searching the banner again for every letter of the note.

Examples

Input:  note = "aab", banner = "baa"
Output: True
Why:    the banner has two a's and one b, exactly what the note needs
Input:  note = "aa", banner = "ab"
Output: False
Why:    the note needs two a's, the banner only has one
Input:  note = "", banner = "xyz"
Output: True
Why:    edge case, an empty note needs no letters at all

Hints

0 / 3

Target time
O(n + m)
Target space
O(1)

Constraints

  • note and banner are lowercase letters
  • each string has at most 10⁵ letters

Stuck on the idea rather than the code? Frequency Counting covers it.

Python 3Ctrl+Enter runs
Custom test case