-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contacts.py
38 lines (31 loc) · 1.03 KB
/
Contacts.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
import collections
def contacts(queries):
data = list()
result = list()
for operation, name in queries:
if operation == 'add':
while len(data) < len(name):
data.append(collections.defaultdict(lambda: set()))
for i, c in enumerate(name):
data[i][c].add(name)
elif operation == 'find':
if len(name) > len(data):
# There is no name stored of that length
result.append(0)
continue
current = None
for i, c in enumerate(name):
if i == 0:
current = data[i][c]
else:
current = current.intersection(data[i][c])
result.append(len(current))
return result
if __name__ == '__main__':
queries_rows = int(input())
queries = []
for _ in range(queries_rows):
queries.append(input().rstrip().split())
result = contacts(queries)
print('\n'.join(map(str, result)))
print('\n')