class trie_node(object):
def __init__(self):
self.node = {}
self.count = 0
def insert(self, word):
curr = self
for c in word:
if not curr.node.get(c, None):
new_node = trie_node()
curr.node[c] = new_node
curr = curr.node[c]
curr.count += 1
def query(self, word):
ret = 0
curr = self
for c in word:
if not curr.node.get(c, None):
return ret
curr = curr.node[c]
ret += curr.count
return ret