Skip to content
BytePatterns

Ransom Note From a Magazine

EasyHash Tables#frequency-count#hash-map~10m

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

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