Skip to main content

Ransom Note

Check if ransomNote can be constructed from magazine Each letter in magazine can only be used once Example: ransomNote = "aa", magazine = "aab" => true Example: ransomNote = "aa", magazine = "ab" => false

I count the magazine letters, then spend them one at a time. The first letter that runs out means the note fails.

Examples

You write

can_construct(ransom_note: str, magazine: str)

  1. Example 1

    Input
    ransomNote = "a", magazine = "b"
    returns
    false
  2. Example 2

    Input
    ransomNote = "aa", magazine = "ab"
    returns
    false

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Ransom Noteeasy