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

Save this for your laptop

The editor and Run need a bigger screen. The link opens Ransom Note ready to code. It is free, and the editor needs no account.

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 126 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 49 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, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.

1Ransom Noteeasy
Based onLeetCode

Get a note when something new lands

New company guides, new problems and big changes to Coco. Rare, and you can leave any time. By joining you agree to get these emails.