10000 Update Solution1, add Solution2 · qilingit/Leetcode@9a77c12 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a77c12

Browse files
committed
Update Solution1, add Solution2
1 parent 7a9a6e1 commit 9a77c12

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
class Solution:
1+
class Solution1:
22
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
33
if ransomNote == magazine:
44
return True
5-
elif len(ransomNote) == len(magazine):
6-
return False
75
else:
86
for r in ransomNote:
97
if r in magazine:
108
magazine = magazine.replace(r, '', 1)
119
else:
1210
return False
1311
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

0 commit comments

Comments
 (0)
0