class Trie {
//定义节点
class TrieNode {
boolean isEnd;
TrieNode[] next = new TrieNode[26];
public TrieNode(){}
}
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 (char c : word.toCharArray()) {
if (curr.next[c - 'a'] == null) {
curr.next[c - 'a'] = new TrieNode();
}
curr = curr.next[c - 'a'];
}
curr.isEnd = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode curr = root;
for (char c : word.toCharArray()) {
if (curr.next[c - 'a'] == null) {
return false;
}
curr = curr.next[c - 'a'];
}
return curr.isEnd;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode curr = root;
for (char c : prefix.toCharArray()) {
if (curr.next[c - 'a'] == null) {
return false;
}
curr = curr.next[c - 'a'];
}
return true;
}
}
/**
* 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);
*/
同类型题目:LeetCode_单词的压缩编码_Array_M