Solution: Ransom Note
Let's solve the Ransom Note problem using the Knowing What to Track pattern.
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.
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach#
A naive approach for this problem is to iterate through each character in the ransom note. For each character, check if it exists in the magazine or not. If it does, we remove that character from the magazine to ensure that each character in the magazine is only used once. If, at any point, we find that a character is not present in the magazine, we return FALSE, indicating that it is not possible to construct a ransom note from the given magazine. If we successfully iterate through all characters in the ransom note, we return TRUE.
We indeed got our desired output. However, this approach is computationally expensive as we have to check the presence of every character of the ransom note in the magazine, giving us the time complexity of
Optimized approach using knowing what to track#
An optimized approach to solve this problem is to keep track of the occurrences of characters using the hash map. We store the frequency of each character of the magazine in the hash map. After storing the frequencies, we iterate over each character in the ransom note and check if the character is present in the hash map and its frequency is greater than zero. If it is, we decrement the frequency by
If we successfully iterate through all characters in the ransom note without encountering a character that is not present in the hash map or its frequency is
Let’s look at the illustration to get a better understanding of the solution.
1 of 11
2 of 11
3 of 11
4 of 11
5 of 11
6 of 11
7 of 11
8 of 11
9 of 11
10 of 11
11 of 11
Let’s implement the algorithm as discussed above:
Solution summary#
Create a hash map to store the frequencies of each character in the magazine.
Iterate through each character in the ransom note and check the following conditions:
If the character is not in the hash map or the frequency of the character is
, return FALSE Otherwise, decrement the frequency of the character in the hash map by
.
Return TRUE if we successfully iterate through all characters in the ransom note.
Time complexity#
The time complexity of this solution is
Space complexity#
The space complexity of this solution is
Ransom Note
Two Sum