Skip to content

Correct Space Complexity and Optimized Solution for LeetCode 383: Ransom Note #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ using namespace std;
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> counter;
for (char c : magazine) {
counter[c]++;
unordered_map<char, int> hashmap;

for (char ch : magazine) {
hashmap[ch]++;
}

for (char c : ransomNote) {
if (counter[c] == 0) {

for (char ch : ransomNote) {
if (hashmap[ch] > 0) {
hashmap[ch]--;
} else {
return false;
}
counter[c]--;
}

return true;
}
};

// Time Complexity: O(m + n) -> m = length of ransomNote, n = length of magazine
// Space Complexity: O(n) -> we're using an unordered_map
20 changes: 12 additions & 8 deletions Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import java.util.HashMap;

public class Solution {
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character, Integer> counter = new HashMap<>();
HashMap<Character, Integer> hashmap = new HashMap<>();

for (char c : magazine.toCharArray()) {
counter.put(c, counter.getOrDefault(c, 0) + 1);
for (char ch : magazine.toCharArray()) {
hashmap.put(ch, hashmap.getOrDefault(ch, 0) + 1);
}

for (char c : ransomNote.toCharArray()) {
if (!counter.containsKey(c) || counter.get(c) == 0) {

for (char ch : ransomNote.toCharArray()) {
if (hashmap.getOrDefault(ch, 0) > 0) {
hashmap.put(ch, hashmap.get(ch) - 1);
} else {
return false;
}
counter.put(c, counter.get(c) - 1);
}

return true;
}
}

// Time Complexity: O(m + n) -> m = len(ransomNote), n = len(magazine)
// Space Complexity: O(n) -> we're using a hashmap
26 changes: 15 additions & 11 deletions Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
function canConstruct(ransomNote, magazine) {
const counter = {};
for (const c of magazine) {
counter[c] = (counter[c] || 0) + 1;
var canConstruct = function(ransomNote, magazine) {
let hashmap = {};

for (let ch of magazine) {
hashmap[ch] = (hashmap[ch] || 0) + 1;
}

for (const c of ransomNote) {
if (!counter[c] || counter[c] === 0) {

for (let ch of ransomNote) {
if (hashmap[ch] > 0) {
hashmap[ch]--;
} else {
return false;
}
counter[c]--;
}

return true;
}
};

// Time Complexity: O(m + n) -> m = length of ransomNote, n = length of magazine
// Space Complexity: O(n) -> we're using a hashmap
21 changes: 6 additions & 15 deletions Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
counter = {}
hashmap = Counter(magazine) # TC for Counter is O(n)

for c in magazine:
if c in counter:
counter[c] += 1
for ch in ransomNote:
if hashmap[ch] > 0:
hashmap[ch]-=1
else:
counter[c] = 1

for c in ransomNote:
if c not in counter:
return False
elif counter[c] == 1:
del counter[c]
else:
counter[c] -= 1

return True

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