To solve the problem of Prefix Search in Trie, we need to determine if any word stored in the Trie starts with a given prefix. This is different from a full word search because we only care whether a word starts with the prefix — not whether a word matches the prefix entirely.
How we approach the problem
A Trie is a tree-like data structure that is particularly efficient for prefix-based queries. Each node in the Trie represents a character, and a path from the root to any node represents a prefix of some word(s).
When performing a prefix search:
- We start from the root of the Trie.
- We go through each character of the prefix one by one.
- For each character, we check whether it exists as a child of the current node.
- If any character is missing, we conclude that no word with that prefix exists in the Trie.
- If we reach the end of the prefix successfully, it means at least one word in the Trie starts with the prefix.
Example:
Suppose the Trie contains: ["app", "apple", "bat", "ball"]
- Prefix to search:
"ap"
- Start at root → Check for 'a' → Exists
- Move to 'a' → Check for 'p' → Exists
- All characters in the prefix found → Return
true
Edge Cases
Case 1 - Empty prefix string
Description: The prefix string is an empty string: ""
- Since every word starts with an empty prefix, we should return
true
.
Example:
Trie: ["a", "ab", "abc"]
Prefix: ""
→ Result: true
Case 2 - Prefix longer than any stored word
Description: The prefix is longer than any word in the Trie.
- Traverse the Trie character by character.
- If at any point the character is not found, return
false
.
Example:
Trie: ["do", "dog", "dot"]
Prefix: "dodge"
→ Result: false
For char 'o', there is no child 'd'. Prefix not present in the trie, hence returning false.
Case 3 - Prefix partially matches a word
Description: The prefix matches the start of a word but does not reach a full word.
- We only care if the prefix matches a path in the Trie.
- If the prefix exists in the Trie path, return
true
.
Example:
Trie: ["go", "gone", "golf"]
Prefix: "gol"
→ Result: true
Case 4 - Prefix not found at all
Description: The prefix has no matching characters in the Trie.
- As soon as we find a character in the prefix that is not in the current node’s children, we return
false
.
Example:
Trie: ["cat", "car", "cart"]
Prefix: "dog"
→ Result: false
Root of the trie has only one child 'c'. First character in given prefix is 'd' and this is not there in the list of children of the root of trie. Therefore, return false.
Case 5 - Multiple words start with the same prefix
Description: The prefix matches the beginning of several words.
- Even if one word starts with the prefix, return
true
.
- Don’t worry about counting how many words — just whether any word matches.
Example:
Trie: ["star", "start", "stark"]
Prefix: "sta"
→ Result: true
Case 6 - Trie is empty
Description: No words are present in the Trie at all.
- If Trie is empty, any prefix will return
false
.
Example:
Trie: []
Prefix: "a"
→ Result: false