Hash Tables: Ransom Note

Hash Tables: Ransom Note

A kidnapper wrote a ransom note but is worried it will be traced back to him. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use whole words available in the magazine, meaning he cannot use substrings or concatenation to create the words he needs.

Given the words in the magazine and the words in the ransom note, print Yes if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No.

Input Format

The first line contains two space-separated integers describing the respective values of $m$ (the number of words in the magazine) and $n$ (the number of words in the ransom note).

The second line contains $m$ space-separated strings denoting the words present in the magazine.
The third line contains $n$ space-separated strings denoting the words present in the ransom note.

Constraints
  • $1 \le m, n \le 30000$
  • $1 \le$ length of my any word $\le 5$
  • Each word consists of English alphabetic letters (i.e., a to z and A to Z ).
  • The words in the note and magazine are case-sensitive.
Output Format

Print Yes if he can use the magazine to create an untraceable replica of his ransom note; otherwise, print No.

Sample Input 0
1
2
3
6 4
give me one grand today night
give one grand today
Sample Output 0
1
Yes
Sample Input 1
1
2
3
6 5
two times three is not four
two times two is four
Sample Output 1
1
No

My Answer Code

1
2
3
4
5
6
7
8
9
10
11
from collections import Counter

def ransom_note(magazine, ransom):

mag_counter = Counter(magazine)
rans_counter = Counter(ransom)

if rans_counter - mag_counter:
return 'No'

return 'Yes'

Test Code

  • Test code 1
1
2
magazine = input().strip().split(' ')
ransom = input().strip().split(' ')
1
2
two times three is not four
two times two is four
1
ransom_note(magazine, ransom)
1
'No'
  • Test code 2
1
2
magazine = input().strip().split(' ')
ransom = input().strip().split(' ')
1
2
give me one grand today night
give one grand today
1
ransom_note(magazine, ransom)
1
'Yes'
Share