Skip to content

Commit 4bf13b2

Browse files
committed
Add ransom note
1 parent 1916607 commit 4bf13b2

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

hashmap/383_ransom_note/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.

hashmap/383_ransom_note/solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def can_construct(self, ransom_note: str, magazine: str) -> bool:
2+
"""
3+
Check if the `ransom_note` can be constructed from the letters of `magazine`.
4+
5+
Each letter in `magazine` can only be used once in `ransom_note`. The function determines
6+
if there are enough characters in `magazine` to form the `ransom_note` (considering character counts).
7+
8+
:param self: Instance reference (used if this is a method in a class).
9+
:param ransom_note: String that needs to be constructed.
10+
:param magazine: String containing available characters.
11+
:return: True if `ransom_note` can be constructed from `magazine`, False otherwise.
12+
"""
13+
hashmap = {}
14+
for character in magazine:
15+
if character not in hashmap:
16+
hashmap[character] = 1
17+
else:
18+
hashmap[character] += 1
19+
for character in ransom_note:
20+
if character not in hashmap or hashmap[character] == 0:
21+
return False
22+
else:
23+
hashmap[character] -= 1
24+
return True
25+
26+
27+
28+
assert can_construct(ransom_note='a', magazine='b') == False, 'Test 1 Failed'
29+
assert can_construct(ransom_note='aa', magazine='ab') == False, 'Test 2 Failed'
30+
assert can_construct(ransom_note='aa', magazine='aab') == True, 'Test 3 Failed'

0 commit comments

Comments
 (0)