-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanConstruct.py
More file actions
31 lines (26 loc) · 809 Bytes
/
canConstruct.py
File metadata and controls
31 lines (26 loc) · 809 Bytes
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
"""
Given two strings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false
otherwise.
Each letter in magazine can only be used once in ransomNote.
Example 1:
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:
Input: ransomNote = "aa", magazine = "aab"
Output: true
"""
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
memo = {}
for i in range(len(magazine)):
if magazine[i] not in memo:
memo[magazine[i]] = 1
else:
memo[magazine[i]] += 1
for j in ransomNote:
if j not in memo:
return False
if j in memo and memo[j] < 1:
return False
memo[j] -= 1
return True