-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path383.RansomNote.py
42 lines (35 loc) · 1.17 KB
/
383.RansomNote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Solution 1
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if len(magazine) < len(ransomNote):
return False
ransomNoteWords = dict()
magzineWords = dict()
for word in ransomNote:
value = ransomNoteWords.get(word, None)
if not value:
ransomNoteWords[word] = 1
else:
ransomNoteWords[word] += 1
for word in magazine:
value = magzineWords.get(word, None)
if not value:
magzineWords[word] = 1
else:
magzineWords[word] += 1
for key in ransomNoteWords.keys():
value = magzineWords.get(key, None)
if not value:
return False
if value < ransomNoteWords[key]:
return False
return True
# Solution 2
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if len(magazine) < len(ransomNote):
return False
for i in set(ransomNote):
if magazine.count(i) < ransomNote.count(i):
return False
return True