-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.py
59 lines (40 loc) · 1.6 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the matchingStrings function below.
# This is a simple hashing question in which we have to count how many times a string comes in the strings list
# Then for each query of string find if it occurs or not
# We will use dictionary to store the count of strings
def matchingStrings(strings, queries):
strings_collection = dict() # dictionary
for s in strings:
if s in strings_collection: # If string is present in dictionary just increase its count
strings_collection[s] += 1
else: # Else just create a dictionary key for it and assign its apperance to 1
strings_collection[s] = 1
ans = []
for q in queries: # For each query find if query_str is there or not and store result in list
if q in strings_collection:
ans.append(strings_collection[q])
else:
ans.append(0)
return ans # return the list
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
strings_count = int(input())
strings = []
for _ in range(strings_count):
strings_item = input()
strings.append(strings_item)
queries_count = int(input())
queries = []
for _ in range(queries_count):
queries_item = input()
queries.append(queries_item)
res = matchingStrings(strings, queries)
fptr.write('\n'.join(map(str, res)))
fptr.write('\n')
fptr.close()