-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contacts.py
76 lines (58 loc) · 1.6 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# https://www.hackerrank.com/challenges/contacts/
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'contacts' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts 2D_STRING_ARRAY queries as parameter.
#
class TrieNode:
def __init__(self) -> None:
self.child = [None] * 26
self.count = 0
class Trie:
def __init__(self) -> None:
self.root = TrieNode()
def insert(self, s):
curr_node: TrieNode = self.root
n = len(s)
for i in range(n):
x = ord(s[i]) - 97
if curr_node.child[x] is None:
curr_node.child[x] = TrieNode()
curr_node = curr_node.child[x]
curr_node.count += 1
def search(self, s):
curr_node = self.root
n = len(s)
for i in range(n):
x = ord(s[i]) - 97
if curr_node.child[x] is None:
return 0
curr_node = curr_node.child[x]
return curr_node.count
def contacts(queries):
res = []
trie = Trie()
for q in queries:
operation, text = q
if operation == "add":
trie.insert(text)
else:
res.append(trie.search(text))
return res
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
queries_rows = int(input().strip())
queries = []
for _ in range(queries_rows):
queries.append(input().rstrip().split())
result = contacts(queries)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()