Skip to content

Commit d31fb20

Browse files
authored
Update Ransom Note - Leetcode 383.py
1 parent dc7557d commit d31fb20

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Ransom Note - Leetcode 383/Ransom Note - Leetcode 383.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Brute Force Solution
2+
class Solution:
3+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
4+
for letter in ransomNote:
5+
if letter in magazine:
6+
position = magazine.index(letter)
7+
magazine = magazine[:position] + magazine[position+1:]
8+
else:
9+
return False
10+
11+
return True
12+
# Time: O(R * M)
13+
# Space: O(1)
14+
15+
# Optimal Solution
116
class Solution:
217
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
318
hashmap = Counter(magazine) # TC for Counter is O(n)
@@ -9,5 +24,5 @@ def canConstruct(self, ransomNote: str, magazine: str) -> bool:
924
return False
1025
return True
1126

12-
# Time Complexity: O(m + n) -> m = len(ransomNote), n = len(magazine)
13-
# Space Complexity: O(n) -> we're using a hashmap
27+
# Time Complexity: O(R + M) -> R = len(ransomNote), M = len(magazine)
28+
# Space Complexity: O(M) -> we're using a hashmap

0 commit comments

Comments
 (0)