diff --git a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.cpp b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.cpp index 64ec264..0e5ed8b 100644 --- a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.cpp +++ b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.cpp @@ -5,19 +5,23 @@ using namespace std; class Solution { public: bool canConstruct(string ransomNote, string magazine) { - unordered_map counter; - - for (char c : magazine) { - counter[c]++; + unordered_map 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 diff --git a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.java b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.java index da44fc0..4bfe6b9 100644 --- a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.java +++ b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.java @@ -1,20 +1,24 @@ import java.util.HashMap; -public class Solution { +class Solution { public boolean canConstruct(String ransomNote, String magazine) { - HashMap counter = new HashMap<>(); + HashMap 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 diff --git a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.js b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.js index 230e100..2410ffd 100644 --- a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.js +++ b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.js @@ -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 diff --git a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py index 9aeeff0..fba5a04 100644 --- a/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py +++ b/Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py @@ -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 \ No newline at end of file