-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctci-ransom-note.py
49 lines (37 loc) · 1.1 KB
/
ctci-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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Hash Tables: Ransom Note
https://www.hackerrank.com/challenges/ctci-ransom-note
"""
import unittest
from collections import Counter
def ransom_note(magazine, ransom):
words = Counter(magazine)
for word in ransom:
if word not in words or words[word] == 0:
return False
else:
words[word] -= 1
return True
if __name__ == '__main__':
m, n = map(int, input().strip().split(' '))
magazine = input().strip().split(' ')
ransom = input().strip().split(' ')
answer = ransom_note(magazine, ransom)
if answer:
print("Yes")
else:
print("No")
class TestRansomNote(unittest.TestCase):
def test_ransom_note(self):
self.assertEqual(
True,
ransom_note("give me one grand today night", "give one grand today")
)
self.assertEqual(
False,
ransom_note("give me one grand today night", "give one grand hello")
)
self.assertEqual(
False,
ransom_note("give me one grand today night", "give one grand grand today")
)