[LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
解法:
Trie(字典树)的知识参见:数据结构之Trie树 和 [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)。
可以采用数组和哈希表的方式实现,代码分别如下:
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); */