|
| 1 | +# Ransom Note |
| 2 | + |
| 3 | +Given two strings, ransom_note and magazine, check if ransom_note can be constructed using the letters from magazine. |
| 4 | +Return TRUE if it can be constructed, FALSE otherwise. |
| 5 | + |
| 6 | +> Note: A ransom note is a written message that can be constructed by using the letters available in the given magazine. |
| 7 | +> The magazine can have multiple instances of the same letter. Each instance of the letter in the magazine can only be |
| 8 | +> used once to construct the ransom note. |
| 9 | +
|
| 10 | +## Constraints |
| 11 | + |
| 12 | +- 1 <= `ransom_note.length`, `magazine.length` <= 10^3 |
| 13 | +- The `ransom_note` and `magazine` consist of lowercase English letters |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Topics |
| 23 | + |
| 24 | +- Hash Table |
| 25 | +- String |
| 26 | +- Counting |
| 27 | + |
| 28 | +## Solution |
| 29 | +An optimized approach to solve this problem is to keep track of the occurrences of characters using the hash map. We |
| 30 | +store the frequency of each character of the magazine in the hash map. After storing the frequencies, we iterate over |
| 31 | +each character in the ransom note and check if the character is present in the hash map and its frequency is greater |
| 32 | +than zero. If it is, we decrement the frequency by 1, indicating that we’ve used that character to construct the ransom |
| 33 | +note. If the character is not present in the hash map or its frequency is 0, we immediately return FALSE since it's |
| 34 | +impossible to construct the ransom note. |
| 35 | + |
| 36 | +If we successfully iterate through all characters in the ransom note without encountering a character that is not present |
| 37 | +in the hash map or its frequency is 0, we return TRUE, indicating that we can construct the ransom note from the |
| 38 | +characters available in the magazine. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +### Summary |
| 53 | + |
| 54 | +- Create a hash map to store the frequencies of each character in the magazine. |
| 55 | +- Iterate through each character in the ransom note and check the following conditions: |
| 56 | + - If the character is not in the hash map or the frequency of the character is 0, return FALSE |
| 57 | + - Otherwise, decrement the frequency of the character in the hash map by 1. |
| 58 | +- Return TRUE if we successfully iterate through all characters in the ransom note. |
| 59 | + |
| 60 | +### Time Complexity |
| 61 | + |
| 62 | +The time complexity of this solution is O(n+m), where n is the length of the ransom note and m is the length of the |
| 63 | +magazine. |
| 64 | + |
| 65 | +### Space Complexity |
| 66 | + |
| 67 | +The space complexity of this solution is O(1) because we have a constant number of lowercase English letters |
| 68 | +(26 unique characters). Therefore, the space required by the hash map will remain constant. |
0 commit comments