Implement a trie with insert , search , and startsWith methods.
Example:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // returns true
trie.search("app"); // returns false
trie.startsWith("app"); // returns true
trie.insert("app");
trie.search("app"); // returns true
Note:
- You may assume that all inputs are consist of lowercase letters
a-z . - All inputs are guaranteed to be non-empty strings.
這題是要實作一個資料結構,叫 Trie (發音同 try),也可叫 Prefix Tree
可以用來在字串裡面,快速搜尋特定的 key
詳細說明可以看這裡
code 如下
Java
Kotlin
沒有留言:
張貼留言