Implement a trie with insert
, search
, and startsWith
methods.html
Note:
You may assume that all inputs are consist of lowercase letters a-z
.數組
解法:數據結構
Trie(字典樹)的知識參見:數據結構之Trie樹 和 [LeetCode] Implement Trie (Prefix Tree) 實現字典樹(前綴樹)。post
能夠採用數組和哈希表的方式實現,代碼分別以下:this
public class Trie { private TrieNode root; /** Initialize your data structure here. */ public Trie() { root = new TrieNode(); } /** Inserts a word into the trie. */ public void insert(String word) { root.insert(word, 0); } /** Returns if the word is in the trie. */ public boolean search(String word) { TrieNode result = root.search(word, 0); return result != null && result.getIsWord(); } /** Returns if there is any word in the trie that starts with the given prefix. */ public boolean startsWith(String prefix) { TrieNode result = root.search(prefix, 0); return result != null; } } class TrieNode { private TrieNode[] children; private boolean isWord; public TrieNode() { children = new TrieNode[26]; isWord = false; } public void insert(String word, int index) { // 若是全部字符都已插入,須要將最後一個字符節點的isWord改成true if (index == word.length()) { this.isWord = true; return; } // 若是不存在該字符,在對應位置新建節點 int n = word.charAt(index) - 'a'; if (children[n] == null) { children[n] = new TrieNode(); } // 繼續下一字符 children[n].insert(word, index + 1); } // 因爲Trie中既要求實現search,又要求實現startsWith,爲了方便, // 此處直接返回搜索結果的TrieNode,交由Trie去判斷。 public TrieNode search(String word, int index) { if (index == word.length()) { return this; } int n = word.charAt(index) - 'a'; if (children[n] == null) { return null; } return children[n].search(word, index + 1); } public boolean getIsWord() { return this.isWord; } } /** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */
public class Trie { private TrieNode root; /** Initialize your data structure here. */ public Trie() { root = new TrieNode(); } /** Inserts a word into the trie. */ public void insert(String word) { TrieNode curr = root; for (int i = 0; i < word.length(); i++) { char letter = word.charAt(i); if (!curr.children.containsKey(letter)) { curr.children.put(letter, new TrieNode()); } curr = curr.children.get(letter); } curr.isWord = true; } /** Returns if the word is in the trie. */ public boolean search(String word) { TrieNode result = find(word); return result != null && result.isWord; } /** Returns if there is any word in the trie that starts with the given prefix. */ public boolean startsWith(String prefix) { TrieNode result = find(prefix); return result != null; } public TrieNode find(String word) { TrieNode curr = root; for (int i = 0; i < word.length(); i++) { char letter = word.charAt(i); if (!curr.children.containsKey(letter)) { return null; } curr = curr.children.get(letter); } return curr; } } class TrieNode { HashMap<Character, TrieNode> children; boolean isWord; public TrieNode() { children = new HashMap<>(); isWord = false; } } /** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */