We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
fromcollectionsimportdefaultdictd=defaultdict(list)n,m=map(int,input().split())### Optimized Version #### Precompute the positions of each value in group_Apositions_dict=defaultdict(list)foriinrange(n):value=input()positions_dict[value].append(i+1)#Store1-basedindexpositions# Check and print the positions for each value in group_Bfor_inrange(m):value=input()ifvalueinpositions_dict:print(*positions_dict[value])#Printallpositionsfoundelse:print(-1)#Print-1ifnooccurrencefound### Less Efficient Version #### for _ in range(n):# d['group_A'].append(input())# for _ in range(m):# d['group_B'].append(input())# for value in d['group_B']:# positions = [i + 1 for i, v in enumerate(d['group_A']) if v == value] # Find all occurrences# if positions:# print(*positions) # Print all positions found# else:# print(-1) # Print -1 if no occurrence found
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
DefaultDict Tutorial
You are viewing a single comment's thread. Return to all comments →