Skip to content

Commit 7ed10fd

Browse files
correct space complexity and simple code
1 parent 0024d62 commit 7ed10fd

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#include <unordered_map>
22
#include <string>
3-
using namespace std;
43

54
class Solution {
65
public:
7-
bool canConstruct(string ransomNote, string magazine) {
8-
unordered_map<char, int> counter;
9-
10-
for (char c : magazine) {
11-
counter[c]++;
6+
bool canConstruct(std::string ransomNote, std::string magazine) {
7+
std::unordered_map<char, int> hashmap;
8+
9+
for (char ch : magazine) {
10+
hashmap[ch]++;
1211
}
13-
14-
for (char c : ransomNote) {
15-
if (counter[c] == 0) {
12+
13+
for (char ch : ransomNote) {
14+
if (hashmap[ch] > 0) {
15+
hashmap[ch]--;
16+
} else {
1617
return false;
1718
}
18-
counter[c]--;
1919
}
20-
20+
2121
return true;
2222
}
2323
};
24+
25+
// Time Complexity: O(m + n) -> m = length of ransomNote, n = length of magazine
26+
// 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)