A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
Trie()
Initializes the trie object.void insert(String word)
Inserts the stringword
into the trie.int countWordsEqualTo(String word)
Returns the number of instances of the stringword
in the trie.int countWordsStartingWith(String prefix)
Returns the number of strings in the trie that have the stringprefix
as a prefix.void erase(String word)
Erases the stringword
from the trie.
Example 1:
Input ["Trie", "insert", "insert", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsEqualTo", "countWordsStartingWith", "erase", "countWordsStartingWith"] [[], ["apple"], ["apple"], ["apple"], ["app"], ["apple"], ["apple"], ["app"], ["apple"], ["app"]] Output [null, null, null, 2, 2, null, 1, 1, null, 0] Explanation Trie trie = new Trie(); trie.insert("apple"); // Inserts "apple". trie.insert("apple"); // Inserts another "apple". trie.countWordsEqualTo("apple"); // There are two instances of "apple" so return 2. trie.countWordsStartingWith("app"); // "app" is a prefix of "apple" so return 2. trie.erase("apple"); // Erases one "apple". trie.countWordsEqualTo("apple"); // Now there is only one instance of "apple" so return 1. trie.countWordsStartingWith("app"); // return 1 trie.erase("apple"); // Erases "apple". Now the trie is empty. trie.countWordsStartingWith("app"); // return 0
Constraints:
1 <= word.length, prefix.length <= 2000
word
andprefix
consist only of lowercase English letters.- At most
3 * 104
calls in total will be made toinsert
,countWordsEqualTo
,countWordsStartingWith
, anderase
. - It is guaranteed that for any function call to
erase
, the stringword
will exist in the trie.
class Trie:
def __init__(self):
self.children = [None] * 26
self.v = self.pv = 0
def insert(self, word: str) -> None:
node = self
for c in word:
idx = ord(c) - ord('a')
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.pv += 1
node.v += 1
def countWordsEqualTo(self, word: str) -> int:
node = self.search(word)
return 0 if node is None else node.v
def countWordsStartingWith(self, prefix: str) -> int:
node = self.search(prefix)
return 0 if node is None else node.pv
def erase(self, word: str) -> None:
node = self
for c in word:
idx = ord(c) - ord('a')
node = node.children[idx]
node.pv -= 1
node.v -= 1
def search(self, word):
node = self
for c in word:
idx = ord(c) - ord('a')
if node.children[idx] is None:
return None
node = node.children[idx]
return node
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.countWordsEqualTo(word)
# param_3 = obj.countWordsStartingWith(prefix)
# obj.erase(word)
class Trie {
private Trie[] children = new Trie[26];
private int v;
private int pv;
public Trie() {
}
public void insert(String word) {
Trie node = this;
for (char c : word.toCharArray()) {
c -= 'a';
if (node.children[c] == null) {
node.children[c] = new Trie();
}
node = node.children[c];
++node.pv;
}
++node.v;
}
public int countWordsEqualTo(String word) {
Trie node = search(word);
return node == null ? 0 : node.v;
}
public int countWordsStartingWith(String prefix) {
Trie node = search(prefix);
return node == null ? 0 : node.pv;
}
public void erase(String word) {
Trie node = this;
for (char c : word.toCharArray()) {
c -= 'a';
node = node.children[c];
--node.pv;
}
--node.v;
}
private Trie search(String word) {
Trie node = this;
for (char c : word.toCharArray()) {
c -= 'a';
if (node.children[c] == null) {
return null;
}
node = node.children[c];
}
return node;
}
}
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* int param_2 = obj.countWordsEqualTo(word);
* int param_3 = obj.countWordsStartingWith(prefix);
* obj.erase(word);
*/
class Trie {
public:
Trie()
: children(26)
, v(0)
, pv(0) {
}
void insert(string word) {
Trie* node = this;
for (char c : word) {
c -= 'a';
if (!node->children[c]) {
node->children[c] = new Trie();
}
node = node->children[c];
++node->pv;
}
++node->v;
}
int countWordsEqualTo(string word) {
Trie* node = search(word);
return node ? node->v : 0;
}
int countWordsStartingWith(string prefix) {
Trie* node = search(prefix);
return node ? node->pv : 0;
}
void erase(string word) {
Trie* node = this;
for (char c : word) {
c -= 'a';
node = node->children[c];
--node->pv;
}
--node->v;
}
private:
vector<Trie*> children;
int v, pv;
Trie* search(string& word) {
Trie* node = this;
for (char c : word) {
c -= 'a';
if (!node->children[c]) {
return nullptr;
}
node = node->children[c];
}
return node;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* int param_2 = obj->countWordsEqualTo(word);
* int param_3 = obj->countWordsStartingWith(prefix);
* obj->erase(word);
*/
type Trie struct {
children [26]*Trie
v int
pv int
}
func Constructor() (_ Trie) { return }
func (this *Trie) Insert(word string) {
node := this
for _, c := range word {
c -= 'a'
if node.children[c] == nil {
node.children[c] = &Trie{}
}
node = node.children[c]
node.pv++
}
node.v++
}
func (this *Trie) CountWordsEqualTo(word string) int {
node := this.search(word)
if node == nil {
return 0
}
return node.v
}
func (this *Trie) CountWordsStartingWith(prefix string) int {
node := this.search(prefix)
if node == nil {
return 0
}
return node.pv
}
func (this *Trie) Erase(word string) {
node := this
for _, c := range word {
c -= 'a'
node = node.children[c]
node.pv--
}
node.v--
}
func (this *Trie) search(word string) *Trie {
node := this
for _, c := range word {
c -= 'a'
if node.children[c] == nil {
return nil
}
node = node.children[c]
}
return node
}
/**
* Your Trie object will be instantiated and called as such:
* obj := Constructor();
* obj.Insert(word);
* param_2 := obj.CountWordsEqualTo(word);
* param_3 := obj.CountWordsStartingWith(prefix);
* obj.Erase(word);
*/