Trie Tree

class TrieNode:
def __init__(self):
self.children = {}  # Dict=> { char:TrieNode }, here char is node val
self.isWord = False

def addWord(self, word):
cur = self
for c in word:
if c not in cur.children:
cur.children[c] = TrieNode ()
cur = cur.children[c]
cur.isWord = True


def searchWord(root: TrieNode, findWord: str = "", idx: int = 0, wordFormed: str = ""):
if findWord == wordFormed and root.isWord:
return True

if idx == len (findWord):
return False

if findWord[idx] not in root.children:
return False
elif root.children[findWord[idx]]:
cur = root.children[findWord[idx]]
return searchWord (cur, findWord, idx + 1, wordFormed + findWord[idx])


root = TrieNode ()
root.addWord ("abc")
root.addWord ("aec")

print (searchWord (root, "a"))
print (searchWord (root, "abc"))
print (searchWord (root, "abcytut"))
print (searchWord (root, "tjddj"))