
You need to implement a trie (prefix tree), a specialized tree data structure used to efficiently store and query strings. This is useful in applications like autocomplete, spell checkers, and string dictionaries.
Your Trie class should support the following methods:
Trie(): Initializes the trie object.void insert(String word): Inserts the string word into the trie. If the word already exists, its instance count increases.int countWordsEqualTo(String word): Returns how many times the exact word exists in the trie.int countWordsStartingWith(String prefix): Returns how many words in the trie start with the given prefix.void erase(String word): Removes one occurrence of word from the trie. If the word exists multiple times, only one instance is erased.Input:
Output:
Explanation:
1 <= word.length, prefix.length <= 2000word and prefix consist only of lowercase English letters.3 * 10^4 calls will be made to all operations combined.erase(word) is only called when word exists in the trie.The trie data structure supports all required operations efficiently by leveraging character-wise traversal and storage:
Initialization:
The root node starts empty. Each node stores:
Insertion:
Traverse the trie character by character.
For each character:
At the final node, increment the end count.
Counting Exact Matches:
word.Counting Prefix Matches:
prefix.Erase:
This design ensures all operations run in O(L) time, where L is the length of the word or prefix, making it efficient even with tens of thousands of operations.
This implementation in C++ provides a detailed example of how to build and manage a Trie (prefix tree), specifically designed for handling operations like adding words, counting exact occurrences of words, counting words that start with a specific prefix, and removing words.
The TrieNode class represents each node in the Trie. Each node includes:
children[26] for storing pointers to child nodes, symbolizing the 26 lowercase letters of the English alphabet.terminalCount, an integer counting how many times a word terminating at this node has been added to the Trie.prefixCount, an integer counting how many words passing through this node have been added to the Trie.The Trie class facilitates various operations:
addWord(string word) inserts a word into the Trie. This function iterates through each character of the word, navigating or creating the necessary child nodes, and increments the prefixCount at each node along the path. At the terminal node of the word, it increments the terminalCount.countExactWords(string word) returns the number of times a complete word was added to the Trie. It traverses the Trie according to the word's characters, returning 0 if a path breaks, or the terminalCount at the final node if it reaches the end.countPrefixWords(string prefix) calculates how many words in the Trie start with a specific prefix by similar traversal as countExactWords, but instead of terminalCount, it returns the prefixCount at the last node.removeWord(string word) decrements the counts related to a previously added word. As it navigates through the Trie following the word's characters, it decrements the prefixCount for each node and terminalCount at the end node.This Trie implementation is efficient for dictionary-like data manipulations where you often need to check for word existence, frequency, and prefix-related queries.
0 Comments
Be the first to comment and share your perspective with the community.