class TrieNode:
def __init__(self):
self.children = {}
self.end_of_word = False
class CamelCaseTrie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
current_node = self.root
for i in range(len(word)):
if word[i].isupper():
prefix = word[:i]
suffix = word[i:]
break
else:
prefix = word
suffix = ""
for char in prefix:
if char not in current_node.children:
current_node.children[char] = TrieNode()
current_node = current_node.children[char]
if suffix:
self._insert_camel_case_word(current_node, suffix)
else:
current_node.end_of_word = True
def _insert_camel_case_word(self, node, word):
current_node = node
for char in word:
if char not in current_node.children:
current_node.children[char] = TrieNode()
current_node = current_node.children[char]
current_node.end_of_word = True
def search(self, word):
current_node = self.root
for i in range(len(word)):
if word[i].isupper():
prefix = word[:i]
suffix = word[i:]
break
else:
prefix = word
suffix = ""
for char in prefix:
if char not in current_node.children:
return False
current_node = current_node.children[char]
if suffix:
return self._search_camel_case_word(current_node, suffix)
else:
return current_node.end_of_word
def _search_camel_case_word(self, node, word):
current_node = node
for char in word:
if char not in current_node.children:
return False
current_node = current_node.children[char]
return current_node.end_of_word
words = ["camelCaseWord", "anotherCamelCaseWord", "yetAnotherCamelCaseWord"]
trie = CamelCaseTrie()
for word in words:
trie.insert(word)
print(trie.search("camelCaseWord")) # Output: True
print(trie.search("anotherCamelCaseWord")) # Output: True
print(trie.search("yetAnotherCamelCaseWord")) # Output: True
print(trie.search("notInTrie")) # Output: False