In this HackerRank DefaultDict Tutorial problem solution, you will be given 2 integers, n, and m. There are m words, which might repeat, in word group A., There are m words belonging to word group B. For each m word, check whether the word has appeared in group A or not. Print the indices of each occurrence of m in group A. If it does not appear, print -1.


HackerRank DefaultDict Tutorial in python problem solution

HackerRank DefaultDict Tutorial in Python problem solution


from collections import defaultdict

d = defaultdict(list)

n,m = map(int,raw_input().split())

for i in xrange(1,n+1):
    d[raw_input()].append(i)
    
for j in xrange(1,m+1):
    letter = raw_input()
    if letter in d:
        print ' '.join(map(str,d[letter]))
    else:
        print -1