File tree Expand file tree Collapse file tree 1 file changed +21
-3
lines changed
May-LeetCoding-Challenge/03-Ransom-Note Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change 1
- class Solution :
1
+ class Solution1 :
2
2
def canConstruct (self , ransomNote : str , magazine : str ) -> bool :
3
3
if ransomNote == magazine :
4
4
return True
5
- elif len (ransomNote ) == len (magazine ):
6
- return False
7
5
else :
8
6
for r in ransomNote :
9
7
if r in magazine :
10
8
magazine = magazine .replace (r , '' , 1 )
11
9
else :
12
10
return False
13
11
return True
12
+
13
+
14
+ class Solution2 :
15
+ def canConstruct (self , ransomNote : str , magazine : str ) -> bool :
16
+ if ransomNote == magazine :
17
+ return True
18
+ else :
19
+ magazines_dict = {}
20
+ for m in magazine :
21
+ if m in magazines_dict :
22
+ magazines_dict [m ] += 1
23
+ else :
24
+ magazines_dict [m ] = 1
25
+
26
+ for r in ransomNote :
27
+ if r in magazines_dict and magazines_dict [r ] > 0 :
28
+ magazines_dict [r ] -= 1
29
+ else :
30
+ return False
31
+ return True
You can’t perform that action at this time.
0 commit comments