Skip to content

Commit ee6f841

Browse files
authored
Merge pull request #9 from Tesfamichael12/main
Correct Space Complexity and Optimized Solution for LeetCode 383: Ransom Note
2 parents c91184e + 9f05be8 commit ee6f841

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ using namespace std;
55
class Solution {
66
public:
77
bool canConstruct(string ransomNote, string magazine) {
8-
unordered_map<char, int> counter;
9-
10-
for (char c : magazine) {
11-
counter[c]++;
8+
unordered_map<char, int> hashmap;
9+
10+
for (char ch : magazine) {
11+
hashmap[ch]++;
1212
}
13-
14-
for (char c : ransomNote) {
15-
if (counter[c] == 0) {
13+
14+
for (char ch : ransomNote) {
15+
if (hashmap[ch] > 0) {
16+
hashmap[ch]--;
17+
} else {
1618
return false;
1719
}
18-
counter[c]--;
1920
}
20-
21+
2122
return true;
2223
}
2324
};
25+
26+
// Time Complexity: O(m + n) -> m = length of ransomNote, n = length of magazine
27+
// Space Complexity: O(n) -> we're using an unordered_map
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import java.util.HashMap;
22

3-
public class Solution {
3+
class Solution {
44
public boolean canConstruct(String ransomNote, String magazine) {
5-
HashMap<Character, Integer> counter = new HashMap<>();
5+
HashMap<Character, Integer> hashmap = new HashMap<>();
66

7-
for (char c : magazine.toCharArray()) {
8-
counter.put(c, counter.getOrDefault(c, 0) + 1);
7+
for (char ch : magazine.toCharArray()) {
8+
hashmap.put(ch, hashmap.getOrDefault(ch, 0) + 1);
99
}
10-
11-
for (char c : ransomNote.toCharArray()) {
12-
if (!counter.containsKey(c) || counter.get(c) == 0) {
10+
11+
for (char ch : ransomNote.toCharArray()) {
12+
if (hashmap.getOrDefault(ch, 0) > 0) {
13+
hashmap.put(ch, hashmap.get(ch) - 1);
14+
} else {
1315
return false;
1416
}
15-
counter.put(c, counter.get(c) - 1);
1617
}
1718

1819
return true;
1920
}
2021
}
22+
23+
// Time Complexity: O(m + n) -> m = len(ransomNote), n = len(magazine)
24+
// Space Complexity: O(n) -> we're using a hashmap
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
function canConstruct(ransomNote, magazine) {
2-
const counter = {};
3-
4-
for (const c of magazine) {
5-
counter[c] = (counter[c] || 0) + 1;
1+
var canConstruct = function(ransomNote, magazine) {
2+
let hashmap = {};
3+
4+
for (let ch of magazine) {
5+
hashmap[ch] = (hashmap[ch] || 0) + 1;
66
}
7-
8-
for (const c of ransomNote) {
9-
if (!counter[c] || counter[c] === 0) {
7+
8+
for (let ch of ransomNote) {
9+
if (hashmap[ch] > 0) {
10+
hashmap[ch]--;
11+
} else {
1012
return false;
1113
}
12-
counter[c]--;
1314
}
14-
15+
1516
return true;
16-
}
17+
};
18+
19+
// Time Complexity: O(m + n) -> m = length of ransomNote, n = length of magazine
20+
// Space Complexity: O(n) -> we're using a hashmap
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
class Solution:
22
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
3-
counter = {}
3+
hashmap = Counter(magazine) # TC for Counter is O(n)
44

5-
for c in magazine:
6-
if c in counter:
7-
counter[c] += 1
5+
for ch in ransomNote:
6+
if hashmap[ch] > 0:
7+
hashmap[ch]-=1
88
else:
9-
counter[c] = 1
10-
11-
for c in ransomNote:
12-
if c not in counter:
139
return False
14-
elif counter[c] == 1:
15-
del counter[c]
16-
else:
17-
counter[c] -= 1
18-
1910
return True
2011

21-
# Time Complexity: O(m + n)
22-
# Space Complexity: O(1)
12+
# Time Complexity: O(m + n) -> m = len(ransomNote), n = len(magazine)
13+
# Space Complexity: O(n) -> we're using a hashmap

0 commit comments

Comments
 (0)