Ransom Note
Try to solve the Ransom Note problem.
We'll cover the following
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.lengthThe
ransom_noteandmagazineconsist of lowercase English letters.
Examples#
1 of 4
2 of 4
3 of 4
4 of 4
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.
Solution: Majority Element
Solution: Ransom Note