-
Notifications
You must be signed in to change notification settings - Fork 0
/
ransom_note.py
34 lines (30 loc) · 996 Bytes
/
ransom_note.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
from collections import Counter
class Solution:
"""
Task:
Given two strings ransomNote and magazine, return true if ransomNote can be constructed
by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote
"""
# Method 1
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
rn, mg = list(ransomNote), list(magazine)
c = len(rn)
i = 0
while i<len(rn):
for j in mg:
if rn[i] == j:
mg.remove(j)
c -= 1
break
i += 1
return c == 0
# Method 2 - MORE EFFICIENT WAY
def canConstruct(self, r: str, m: str) -> bool:
for i in set(r):
if i not in m or r.count(i)>m.count(i):
return False
return True
# Method 3
def canConstruct(self, r: str, m: str) -> bool:
return (Counter(r) - Counter(m)) == {}