Search
⌘K
Get Premium
Trie

Prefix Matching

medium

DESCRIPTION

Use a Trie to implement a prefix matching function.

  • match(prefix) returns a list of all words in the Trie that start with the given prefix. The words can be in any order.

The creation of the Trie is already implemented for you.

The test cases include two parameters:

  • words: a list of words to add to the Trie,
  • prefix: a prefix to search for.

The test cases will create the Trie with the initial words, and then run the match command, and compare the output to the expected output.

Example 1:

Input:

initialWords = ["apple", "app", "apartment", "ap", "apricot"]
prefix = "app"

Output: ["apple", "app"]

Example 2:

Input:

initialWords = ["ball", "bath", "bat", "batter"]
prefix = "bat"

Output: ["bat", "bath", "batter"]

Explanation

We need to use the Trie to find all words that have a given prefix. The intuition is to search for the prefix in the trie and then perform depth-first search to find all words that have the given prefix.
The animation below visualizes what that looks like:
Prefix: BAELPPASTLLABresult[]

Step 1: Search for the Prefix

The first step is to search for the prefix in the trie. We start from the root node and traverse down the trie to find the node that corresponds to the prefix. If the prefix does not exist in the trie, we return an empty list. We stop at the node that corresponds to the prefix.
Solution
def prefix(self, word):
"""
Return a list of all words in the trie
that start with the given prefix.
"""
node = self.root
for char in word:
if char not in node.children:
return [] # Prefix not found in trie
node = node.children[char]
After finding the node that corresponds to the prefix, we perform a depth-first search to find all words that have the given prefix.
We can introduce a helper function dfs that takes the current node and the current word as arguments. Each call to dfs will explore the children of the current node and append the characters to the current word. When we reach the end of a word, we add the current word to the result list, which we will return at the end.
Solution
def prefix(self, word):
"""
Return a list of all words in the trie that start with the given prefix.
"""
node = self.root
for char in word:
if char not in node.children:
return [] # Prefix not found in trie
node = node.children[char]
# Now perform DFS to collect all words
result = []
def dfs(current_node, current_word):
if current_node.isEndOfWord:
result.append(current_word)
for char, child_node in current_node.children.items():
dfs(child_node, current_word + char)
dfs(node, word)
return result
Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(N + M) where N is the length of the prefix and M is the number of characters in the words that have the given prefix. We search for the prefix in the trie in O(N) time and perform a depth-first search to find all words in O(M) time.

Space Complexity: O(M) where M is the number of characters in matching words. The space complexity is dominated by the output list that stores all words matching the given prefix, while the call stack space is only proportional to the length of the longest matching word.

If N is the length of the prefix and M is the number of characters in the words that match the prefix, the space complexity is O(x * N + M) for the output list, where x is the number of words that match the prefix.
The exact calculation of this space complexity is not as important as understanding why the output list dominates the space complexity.

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Reading Progress

On This Page