Trie

Trie(字典樹)的實作與題目,4 題。前綴共享結構,處理字串集合查詢時的最佳解。

Implement Trie II 解題教學示意圖|LeetCode 1804

Implement Trie II 解題教學:3 種解法完整比較|LeetCode 1804 Best Practice

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.

Implement Trie II 解題教學:3 種解法完整比較|LeetCode 1804 Best Practice Read More »

Leetcode, 技術文章, , , ,
Design Add and Search Words Data Structure 解題教學示意圖|LeetCode 211

Design Add and Search Words Data Structure 解題教學:3 種解法完整比較|LeetCode 211 Best Practice

Design a data structure that supports adding new words and finding if a string matches any previously added string.

Implement the WordDictionary class:

WordDictionary() Initializes the object.
void addWord(word) Adds word to the data structure, it can be matched later.
bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots ‘.’ where dots can be matched with any letter.

Design Add and Search Words Data Structure 解題教學:3 種解法完整比較|LeetCode 211 Best Practice Read More »

Leetcode, 技術文章, , , ,
Implement Trie 解題教學示意圖|LeetCode 208

Implement Trie 解題教學:3 種解法完整比較|LeetCode 208 Best Practice

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.
boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.

Implement Trie 解題教學:3 種解法完整比較|LeetCode 208 Best Practice Read More »

技術文章, Leetcode, , , ,
Word Search II 解題教學示意圖|LeetCode 212

Word Search II 解題教學:3 種解法完整比較|LeetCode 212 Best Practice

Given an m x n board of characters and a list of strings words, return all words on the board.

Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

Word Search II 解題教學:3 種解法完整比較|LeetCode 212 Best Practice Read More »

Leetcode, 技術文章, , , ,