Ransom Note
Try to solve the Ransom Note problem.
Statement
Given two strings, ransom_note
and magazine
, check if ransom_note
can be constructed using the letters from magazine
. Return TRUE if it can be constructed, FALSE otherwise.
Note: A ransom note is a written message that can be constructed by using the letters available in the given magazine. The magazine can have multiple instances of the same letter. Each instance of the letter in the magazine can only be used once to construct the ransom note.
Constraints:
ransom_note.length
,magazine.length
The
ransom_note
andmagazine
consist of lowercase English letters.
Examples
Understanding the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Ransom Note
For the following strings, can we generate the ransom note using the letters from the magazine string?
ransom note = “problemsolving”
magazine = “adsoptendroblemfemopvinxtbml”
Yes
No
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Note: As an additional challenge, we have intentionally hidden the solution to this puzzle.
Try it yourself
Implement your solution in the following coding playground.
We have left the solution to this challenge as an exercise for you. An optimal solution to this problem runs in O(n + m) time and takes O(1) space. You may try to translate the logic of the solved puzzle into a coded solution.
def can_construct(ransom_note, magazine):# Replace this placeholder return statement with your codereturn False