|
| 1 | +# Ransom Note |
| 2 | + |
| 3 | +## Problem Description |
| 4 | + |
| 5 | +Given two strings `ransomNote` and `magazine`, return `true` if `ransomNote` can be constructed by using the letters from `magazine` and `false` otherwise. |
| 6 | + |
| 7 | +Each letter in `magazine` can only be used once in `ransomNote`. |
| 8 | + |
| 9 | +**Example 1:** |
| 10 | + |
| 11 | +Input: `ransomNote = "a"`, `magazine = "b"` |
| 12 | +Output: `false` |
| 13 | + |
| 14 | +**Example 2:** |
| 15 | + |
| 16 | +Input: `ransomNote = "aa"`, `magazine = "ab"` |
| 17 | +Output: `false` |
| 18 | + |
| 19 | +**Example 3:** |
| 20 | + |
| 21 | +Input: `ransomNote = "aa"`, `magazine = "aab"` |
| 22 | +Output: ``true` |
| 23 | + |
| 24 | +**Constraints:** |
| 25 | + |
| 26 | +* `1 <= ransomNote.length, magazine.length <= 105` |
| 27 | +* `ransomNote` and `magazine` consist of lowercase English letters. |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +```python |
| 32 | +def can_construct(self, ransom_note: str, magazine: str) -> bool: |
| 33 | + """ |
| 34 | + Check if the `ransom_note` can be constructed from the letters of `magazine`. |
| 35 | +
|
| 36 | + Each letter in `magazine` can only be used once in `ransom_note`. The function determines |
| 37 | + if there are enough characters in `magazine` to form the `ransom_note` (considering character counts). |
| 38 | +
|
| 39 | + :param self: Instance reference (used if this is a method in a class). |
| 40 | + :param ransom_note: String that needs to be constructed. |
| 41 | + :param magazine: String containing available characters. |
| 42 | + :return: True if `ransom_note` can be constructed from `magazine`, False otherwise. |
| 43 | + """ |
| 44 | + hashmap = {} |
| 45 | + for character in magazine: |
| 46 | + if character not in hashmap: |
| 47 | + hashmap[character] = 1 |
| 48 | + else: |
| 49 | + hashmap[character] += 1 |
| 50 | + for character in ransom_note: |
| 51 | + if character not in hashmap or hashmap[character] == 0: |
| 52 | + return False |
| 53 | + else: |
| 54 | + hashmap[character] -= 1 |
| 55 | + return True |
| 56 | +``` |
| 57 | + |
| 58 | +* **Time Complexity:** $O(m + n)$ |
| 59 | +* **Space Complexity:** $O(1)$ |
| 60 | + |
| 61 | +## Explanation of the Solution |
| 62 | + |
| 63 | +Step-by-Step Breakdown: |
| 64 | + |
| 65 | +1. Count Characters in magazine: |
| 66 | + |
| 67 | + * A hashmap (dictionary) is used to store the count of each character in magazine. |
| 68 | + * For each character in magazine: |
| 69 | + * If the character is not in the hashmap, add it with a count of 1. |
| 70 | + * If the character exists, increment its count by 1. |
| 71 | + |
| 72 | + **Example:** |
| 73 | + `magazine = "aab"` → `hashmap = {'a': 2, 'b': 1}` |
| 74 | + |
| 75 | +2. Check Characters in ransom_note: |
| 76 | + * For each character in ransom_note: |
| 77 | + * If the character is missing in the hashmap or its count is 0, return False (cannot construct the note). |
| 78 | + * Otherwise, decrement the count of that character in the hashmap (to mark it as "used"). |
| 79 | +3. Return True if All Checks Pass: |
| 80 | + * If the loop completes without issues, ransom_note can be constructed → Return True. |
0 commit comments