Given two strings ransomNote
and magazine
, return true
if ransomNote
can be constructed by using the letters from magazine
and false
otherwise.
Each letter in magazine
can only be used once in ransomNote
.
Example 1:
Input: ransomNote = "a", magazine = "b" Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab" Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab" Output: true
Constraints:
1 <= ransomNote.length, magazine.length <= 105
ransomNote
and magazine
consist of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks if we can construct a ransom note using letters from a magazine. The brute force approach directly tries to 'use' each letter in the magazine to build the ransom note, one letter at a time, checking if we have enough.
Here's how the algorithm would work step-by-step:
def can_construct_ransom_note(ransom_note, magazine):
magazine_letters = list(magazine)
for ransom_note_letter in ransom_note:
# Iterate through each letter in ransom note
found_match = False
for magazine_letter_index in range(len(magazine_letters)):
if magazine_letters[magazine_letter_index] == ransom_note_letter:
# If we find the letter, mark it and break
magazine_letters.pop(magazine_letter_index)
found_match = True
break
if not found_match:
# If letter not found, note can't be constructed
return False
return True
The most efficient way to solve this problem is to count how many times each letter appears in the magazine. Then, we check if we have enough of each letter to create the ransom note. If we do, we can make the note; otherwise, we cannot.
Here's how the algorithm would work step-by-step:
def canConstruct(ransomNote, magazine):
magazine_letter_counts = {}
for letter in magazine:
magazine_letter_counts[letter] = magazine_letter_counts.get(letter, 0) + 1
# We will iterate over the ransom note.
for letter in ransomNote:
if letter not in magazine_letter_counts or magazine_letter_counts[letter] == 0:
return False
# Decrement the count after processing a letter
magazine_letter_counts[letter] -= 1
return True
Case | How to Handle |
---|---|
ransomNote is an empty string | Return true because an empty ransomNote can always be constructed from any magazine. |
magazine is an empty string and ransomNote is not | Return false because ransomNote cannot be constructed from an empty magazine. |
Both ransomNote and magazine are empty strings | Return true because an empty ransomNote can always be constructed from an empty magazine. |
ransomNote is longer than magazine | Return false because ransomNote cannot be constructed if it's longer than magazine. |
ransomNote contains characters not present in magazine | Return false because the ransomNote cannot be constructed with missing characters. |
ransomNote requires more occurrences of a character than magazine provides | The frequency count check will ensure that the solution returns false if sufficient instances do not exist. |
Large input strings that could cause performance issues | Using a hash map/frequency counter approach ensures O(m+n) time complexity, which scales linearly with input size. |
Input strings contain non-ASCII characters (Unicode) | Hash map handles Unicode characters correctly as keys, maintaining correct character counts. |