Skip to content

Latest commit

 

History

History
96 lines (67 loc) · 2.27 KB

File metadata and controls

96 lines (67 loc) · 2.27 KB

https://www.hackerrank.com/challenges/defaultdict-tutorial/problem

PreparePythonCollectionsDefaultDict Tutorial DefaultDict Tutorial

52/115 challenges solved Rank: 99458|Points: 799.04// https://www.hackerrank.com/scoring Python

The /defaultdict/ tool is a container in the collections class of Python. It's similar to the usual dictionary (/dict/) container, but the only difference is that a defaultdict will have a /default/ value if that key has not been set yet. If you didn't use a defaultdict you'd have to check to see if that key exists, and if it doesn't, set it to what you want. For example:

from collections import defaultdict
d = defaultdict(list)
d['python'].append("awesome")
d['something-else'].append("not relevant")
d['python'].append("language")
for i in d.items():
    print (i)

This prints:

|('python', ['awesome', 'language']) ('something-else', ['not relevant']) |

In this challenge, you will be given 2 integers, N and M. There are N words, which might repeat, in word group A. There are M words belonging to word group B. For each M words, 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.

Example

Group A contains 'a', 'b', 'a' Group B contains 'a', 'c'

For the first word in group B, 'a', it appears at positions 1 and 3 in group A. The second word, 'c', does not appear in group A, so print .

Expected output:

1 3
-1

Input Format

The first line contains 2 integers, N and M separated by a space. The next N lines contains the words belonging to group A. The next M lines contains the words belonging to group B.

Constraints 0 < N < 100001

0 < M < 101

0 < length of each word < 101

Output Format

Output M lines. The ith line should contain the 1-indexed positions of the occurrences of the ith word separated by spaces.

Sample Input

| STDIN | Function |


5 2 #group A size n = 5, group B size m = 2
a #group A contains 'a', 'a', 'b', 'a', 'b'
a
b
a
b
a #group B contains 'a', 'b'
b

Sample Output

1 2 4
3 5

Explanation

'a' appeared times in positions 1, 2vand 4. 'b' appeared times in positions 3 and 5. In the sample problem, if 'c' also appeared in word group , you would print -1.