...

/

Ransom Note

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:

  • 11 \leq ransom_note.length , magazine.length 103\leq 10^3

  • The ransom_note and magazine 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

1.

For the following strings, can we generate the ransom note using the letters from the magazine string?

ransom note = “problemsolving”

magazine = “adsoptendroblemfemopvinxtbml”

A.

Yes

B.

No


1 / 4

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.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

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.

Python
usercode > main.py
def can_construct(ransom_note, magazine):
# Replace this placeholder return statement with your code
return False
Ransom Note

Access this course and 1200+ top-rated courses and projects.