-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path383. Ransom Note.py
More file actions
46 lines (33 loc) · 1.05 KB
/
383. Ransom Note.py
File metadata and controls
46 lines (33 loc) · 1.05 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
"""
https://leetcode.com/problems/ransom-note/
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.
Example 1:
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab"
Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab"
Output: true
"""
from collections import Counter
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
r_counter = Counter(ransomNote)
m_counter = Counter(magazine)
for letter, count in r_counter.items():
if m_counter.get(letter, 0) < count:
return False
return True
solution = Solution()
assert solution.canConstruct("a", "b") is False
assert solution.canConstruct("aa", "ab") is False
assert solution.canConstruct("aa", "aab") is True