Skip to content
the poor coder

Leetcode - Ransom Note Solution

Poor Man
Published
Updated
Reading time
1 min read

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

Example 1:

Text
Input: ransomNote = "a", magazine = "b"
Output: false

Example 2:

Text
Input: ransomNote = "aa", magazine = "ab"
Output: false

Example 3:

Text
Input: ransomNote = "aa", magazine = "aab"
Output: true

Constraints:

  • You may assume that both strings contain only lowercase letters.

Solution in Python#

Python
from collections import Counter
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        return not any((Counter(ransomNote)-Counter(magazine)).values())

Keep going

2021

Leetcode - Camelcase Matching Solution

A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it equals the query. (We may insert each character at any position, and may insert 0 characters.) Given a list of…

ESC