-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0383_ransom_note.py
More file actions
46 lines (37 loc) · 1.34 KB
/
0383_ransom_note.py
File metadata and controls
46 lines (37 loc) · 1.34 KB
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
43
44
45
46
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if (len(ransomNote) > len(magazine)):
return False
elif (ransomNote == ''):
return True
ransomDict = {}
# Convert magazine into a dictionary
for char in magazine:
ransomDict[char] = ransomDict.get(char, 0) + 1
for char in ransomNote:
# If char of ransomNote in magazine
if (ransomDict.get(char, 0) != 0):
ransomDict[char] -= 1
# If char of ransomNote not in magazine
else:
return False
return True
'''
# Using While Loop
if (len(ransomNote) > len(magazine)):
return False
elif (ransomNote == ''):
return True
# Convert string into an array
ransomArray = list(ransomNote)
i = 0
while (i < len(magazine) and len(ransomArray) > 0):
# Element in magazine found in ransomArray
if (magazine[i] in ransomArray):
ransomArray.remove(magazine[i])
i += 1
# All characters in ransomNote are in magazine
if (len(ransomArray) == 0):
return True
return False
'''