Skip to content

Instantly share code, notes, and snippets.

@djaquels
Created July 12, 2021 01:16
Show Gist options
  • Save djaquels/21e03a6823379ffe6bb01ab91467271f to your computer and use it in GitHub Desktop.
Save djaquels/21e03a6823379ffe6bb01ab91467271f to your computer and use it in GitHub Desktop.
import sys
class Trie:
def __init__(self):
self.childs = {}
self.isEnd = False
def addWord(self,word):
current = self
for w in word:
if w not in current.childs:
current.childs[w] = Trie()
current = current.childs[w]
current.isEnd = True
def searchWord(self,word):
current = self
for w in word:
if w not in current.childs:
return False
else:
current = current.childs[w]
return current.isEnd
queries = int(input())
trie = Trie()
for nquery in range(queries):
query = input()
tquery,word = query.strip().split(" ")
if tquery == "1":
trie.addWord(word)
if tquery == "2":
if trie.searchWord(word):
print("TRUE")
else:
print("FALSE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment