319/670

319. Ransom Note

Easy

You are given two strings of lowercase letters: note, the message you want to assemble, and magazine, the pool of letters you may cut out. Each letter cut from magazine can be glued into the note once — using a letter consumes it.

Return true if every character of note can be covered by a distinct character of magazine, and false otherwise. Multiplicities matter: if the note needs three as, the magazine must supply at least three as. Order never matters.

Example 1:

Input: note = "aab", magazine = "baa"

Output: true

Explanation: The magazine offers two a's and one b — exactly the letters the note needs, just in a different order.

Example 2:

Input: note = "aa", magazine = "ab"

Output: false

Explanation: The note needs two a's but the magazine only contains one; the b cannot stand in for an a.

Constraints:

  • 1 ≤ note.length ≤ 10⁵
  • 1 ≤ magazine.length ≤ 10⁵
  • note and magazine consist of lowercase English letters only.

Hints:

Order is irrelevant — only how many times each letter appears. What structure answers "how many of this letter do I have" in O(1)?

Tally magazine into a 26-slot count array. Then walk the note decrementing the matching slot; the moment a slot would go below zero, the answer is false.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: note = "aab", magazine = "baa"

Expected output: true