Welcome to Subscribe On Youtube

1804. Implement Trie II (Prefix Tree)

Description

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 string word into the trie.
  • int countWordsEqualTo(String word) Returns the number of instances of the string word in the trie.
  • int countWordsStartingWith(String prefix) Returns the number of strings in the trie that have the string prefix as a prefix.
  • void erase(String word) Erases the string word 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 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 string word will exist in the trie.

Solutions

Solution 1: Implement Trie with Array

Each node in the Trie includes three parts:

  1. An array of pointers children pointing to child nodes. For this problem, the array length is 26, which is the number of lowercase English letters. children[0] corresponds to the lowercase letter a, …, children[25] corresponds to the lowercase letter z.
  2. An int variable v, representing the number of strings ending with this node.
  3. An int variable pv, representing the number of strings with this node as the prefix node.

1. Insert String

We start from the root of the Trie and insert the string. For the child node corresponding to the current character, there are two cases:

  • The child node exists. Move to the child node along the pointer and continue to process the next character.
  • The child node does not exist. Create a new child node, record it in the corresponding position of the children array, then move to the child node along the pointer, and increase the pv value of the child node by 1. Continue to search for the next character.

Repeat the above steps until the last character of the string is processed, then increase the v value of the current node by 1.

The time complexity is $O(n)$, where $n$ is the length of the string.

2. Search Prefix

We start from the root of the Trie and search for the prefix. For the child node corresponding to the current character, there are two cases:

  • The child node exists. Move to the child node along the pointer and continue to search for the next character.
  • The child node does not exist. This means that the Trie does not contain this prefix, return a null pointer.

Repeat the above steps until a null pointer is returned or the last character of the prefix is searched.

The time complexity is $O(n)$, where $n$ is the length of the string.

3. Remove String

We start from the root node of the Trie, and sequentially reduce the pv value of the corresponding child node by 1, until the last character of the string is searched. Then reduce the v value of the current node by 1.

The time complexity is $O(n)$, where $n$ is the length of the string.

  • 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);
     */
    
  • 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)
    
    
  • 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);
     */
    

All Problems

All Solutions