forked from marcosdeseul/algorithm-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
42 lines (36 loc) · 1.18 KB
/
solution.py
File metadata and controls
42 lines (36 loc) · 1.18 KB
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
def char_index(char):
return ord(char) - ord('a')
class TrieNode:
LENGTH = 26
def __init__(self, data=None):
self.data = data
self.children = [None] * 26
self.count = 1
class Trie:
def __init__(self):
self.root = TrieNode()
def add(self, word):
current = self.root
for ch in word:
if current.children[char_index(ch)] is None:
current.children[char_index(ch)] = TrieNode(data=ch)
else:
current.children[char_index(ch)].count += 1
current = current.children[char_index(ch)]
def count(self, keyword):
current = self.root
for ch in keyword:
current = current.children[char_index(ch)]
if current is None:
return 0
return current.count
if __name__ == "__main__":
with open("input-01.txt") as f:
n_line = int(f.readline())
trie = Trie()
for _ in range(n_line):
type_, word_ = f.readline().strip().split(" ")
if type_ == "add":
trie.add(word_)
else:
print(trie.count(word_))