Created
July 12, 2021 01:16
-
-
Save djaquels/21e03a6823379ffe6bb01ab91467271f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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