diff --git a/DSA/Python/Hashing/Problem Statement b/DSA/Python/Hashing/Problem Statement new file mode 100644 index 0000000..e78345a --- /dev/null +++ b/DSA/Python/Hashing/Problem Statement @@ -0,0 +1,25 @@ +Harold is a kidnapper who wrote a ransom note, but now he is worried it will be traced back to him through his handwriting. 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 only whole words available in the magazine. 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. + +Example + magazine= "attack at dawn" note= "Attack at dawn" + +The magazine has all the right words, but there is a case mismatch. The answer is No. + +Function Description + +Complete the checkMagazine function in the editor below. It must print Yes if the note can be formed using the magazine, or No. + +checkMagazine has the following parameters: + +string magazine[m]: the words in the magazine +string note[n]: the words in the ransom note +Prints + +string: either Yes or No , no return value is expected +Input Format + +The first line contains two space-separated integers, m and n, the numbers of words in the magazine and the note, respectively. +The second line contains m space-separated strings, each magazine[i]. +The third line contains n space-separated strings, each note[i] diff --git a/DSA/Python/Hashing/Solution b/DSA/Python/Hashing/Solution new file mode 100644 index 0000000..751567e --- /dev/null +++ b/DSA/Python/Hashing/Solution @@ -0,0 +1,33 @@ +#!/bin/python3 + +import math +import os +import random +import re +import sys + +# Complete the checkMagazine function below. +def checkMagazine(magazine, note): + dict = {} + for word in magazine: + dict[word] = dict.get(word,0) + 1 + for word in note: + if dict.get(word,0) == 0: + print('No') + return + else: + dict[word] -= 1 + print('Yes') + +if __name__ == '__main__': + mn = input().split() + + m = int(mn[0]) + + n = int(mn[1]) + + magazine = input().rstrip().split() + + note = input().rstrip().split() + + checkMagazine(magazine, note)