Implement Trie II (Prefix Tree) - Problem
A Trie (pronounced as "try") or prefix tree is a tree data structure that efficiently stores and retrieves keys in a dataset of strings. Think of it as a digital dictionary that can instantly tell you not just if a word exists, but also how many times it appears and how many words start with a given prefix!
This enhanced version of the classic Trie supports:
insert(word)- Add a word to the trie (can be added multiple times)countWordsEqualTo(word)- Count how many instances of this exact word existcountWordsStartingWith(prefix)- Count how many words start with this prefixerase(word)- Remove one instance of this word from the trie
Real-world applications: Autocomplete systems (like Google Search), spell checkers, word frequency analyzers, and search engines all rely on similar data structures!
Input & Output
example_1.py โ Basic Operations
$
Input:
trie = Trie()
trie.insert("apple")
trie.insert("apple")
trie.countWordsEqualTo("apple") // return 2
trie.countWordsStartingWith("app") // return 2
trie.erase("apple")
trie.countWordsEqualTo("apple") // return 1
โบ
Output:
2, 2, 1
๐ก Note:
We insert "apple" twice, so countWordsEqualTo returns 2. Both instances start with "app", so countWordsStartingWith returns 2. After erasing once, only 1 instance remains.
example_2.py โ Prefix Counting
$
Input:
trie = Trie()
trie.insert("car")
trie.insert("card")
trie.insert("care")
trie.insert("cat")
trie.countWordsStartingWith("car") // return 3
trie.countWordsStartingWith("ca") // return 4
โบ
Output:
3, 4
๐ก Note:
Three words start with "car" (car, card, care), and four words start with "ca" (all of them including cat).
example_3.py โ Edge Cases
$
Input:
trie = Trie()
trie.countWordsEqualTo("missing") // return 0
trie.countWordsStartingWith("x") // return 0
trie.erase("nonexistent") // no effect
trie.insert("a")
trie.erase("a")
trie.countWordsEqualTo("a") // return 0
โบ
Output:
0, 0, 0
๐ก Note:
Querying non-existent words returns 0. Erasing non-existent words has no effect. After inserting and erasing "a", the count becomes 0.
Constraints
- 1 โค word.length, prefix.length โค 2000
- word and prefix consist only of lowercase English letters
- At most 3 ร 104 calls in total will be made to insert, countWordsEqualTo, countWordsStartingWith, and erase
- It is guaranteed that for any function call to erase, the word will exist in the trie
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Empty Trie
Start with root node containing empty counters
2
Insert 'car'
Create path cโaโr, increment prefix counts along path, set word count at end
3
Insert 'card'
Reuse existing cโaโr path, extend with 'd', update all counters
4
Query prefix 'car'
Navigate to 'r' node and return its prefix count
Key Takeaway
๐ฏ Key Insight: By storing word count and prefix count at each node, the Trie enables O(m) operations instead of O(nรm) linear search, making it highly efficient for prefix-based queries.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code