We need to implement a trie data structure for efficient string matching. The user likely expects code, but no specific language is mentioned. Usually, such questions ask to implement in Python, but could be generic. Since it's a system instruction: "Implement a trie data structure for efficient string matching." Possibly they want a code implementation. We need to produce a solution: define a Trie class with methods: insert, search, startsWith, maybe delete. Provide explanation? The instruction says "Implement a trie data structure for efficient string matching." It might be a coding task. We'll produce a Python implementation. Also, consider the format: It's a ChatGPT conversation, but we're the assistant. So we need to respond with the implementation. Possibly also explain usage.
But note the system instruction: "You are ChatGPT ..." and then user says: "Implement a trie data structure for efficient string matching." So we should output code with explanation. We'll do that.
We'll implement a TrieNode class and Trie class. Use dictionary for children. Include methods: insert, search, startsWith. Could also include delete if needed, but not required.
We'll provide a clean code.
We'll also consider memory efficiency and typical operations.
Let's write:
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word: str) -> None:
node = self.root
for ch in word:
if ch not in node.children:
node.children[ch] = TrieNode()
node = node.children[ch]
node.is_end = True
def search(self, word: str) -> bool:
node = self.root
for ch in word:
if ch not in node.children:
return False
node = node.children[ch]
return node.is_end
def startsWith(self, prefix: str) -> bool:
node = self.root
for ch in prefix:
if ch not in node.children:
return False
node = node.children[ch]
return True
That's the basic. Also could implement a delete method, but not required.
If they want efficient string matching, maybe also include a method for pattern matching? But typical trie does prefix matching. So we can mention it's used for autocomplete, spell check, etc.
We can also provide an example usage:
trie = Trie()
trie.insert("apple")
print(trie.search("apple")) # True
print(trie.search("app")) # False
print(trie.startsWith("app")) # True
We should include docstrings.
Optionally, we can implement a more memory-efficient version using arrays, but dictionary is fine.
We'll output the code with explanation.
Thus final answer.
Below is a Python implementation of a trie (prefix tree) that supports efficient insertion, exact word search, and prefix checking. It uses a dictionary for child nodes to keep the implementation simple and flexible.
class TrieNode:
"""Node of the trie."""
def __init__(self):
self.children = {} # mapping from character to TrieNode
self.is_end = False # marks the end of a word
class Trie:
"""Trie data structure for efficient string matching."""
def __init__(self):
self.root = TrieNode()
def insert(self, word: str) -> None:
"""Insert a word into the trie."""
node = self.root
for ch in word:
if ch not in node.children:
node.children[ch] = TrieNode()
node = node.children[ch]
node.is_end = True
def search(self, word: str) -> bool:
"""Return True if the exact word is in the trie, else False."""
node = self.root
for ch in word:
if ch not in node.children:
return False
node = node.children[ch]
return node.is_end
def startsWith(self, prefix: str) -> bool:
"""Return True if there is any word in the trie that starts with the prefix."""
node = self.root
for ch in prefix:
if ch not in node.children:
return False
node = node.children[ch]
return True
Example usage:
trie = Trie()
trie.insert("apple")
print(trie.search("apple")) # True
print(trie.search("app")) # False
print(trie.startsWith("app")) # True
This trie allows O(L) time per operation, where L is the length of the word/prefix, making it highly efficient for tasks like autocomplete, spell checking, and prefix-based searches.