trie字典树可以用来查找单词或者搜索剪枝用。
Implement Trie (Prefix Tree) 实现一个 Trie,包含 insert
, search
, 和 startsWith
这三个方法。(模板必须记住;没有儿子建立儿子,有儿子走儿子;)
class Trie {
private class TrieNode {
public TrieNode[] children;
public boolean isword;
public TrieNode () {
this.children = new TrieNode[26];
this.isword = false;
}
}
/** Initialize your data structure here. */
private TrieNode root;
public Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
if(word == null || word.length() == 0) {
return;
}
TrieNode cur = root;
for(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(cur.children[c - 'a'] == null) {
cur.children[c - 'a'] = new TrieNode();
}
cur = cur.children[c - 'a'];
}
cur.isword = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode cur = searchPrefix(word);
return cur != null && cur.isword;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode cur = searchPrefix(prefix);
return cur != null;
}
private TrieNode searchPrefix(String prefix) {
if(prefix == null || prefix.length() == 0) {
return null;
}
TrieNode cur = root;
for(int i = 0; i < prefix.length(); i++) {
char c = prefix.charAt(i);
if(cur.children[c - 'a'] == null) {
return null;
}
cur = cur.children[c - 'a'];
}
return cur;
}
}
/**
* 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);
*/
Add and Search Word - Data structure design (遇见 ‘.’ 之后,for循环check每一个可能性;注意这题我写了两个坑:
1. index == word.length()的时候,返回的是cur.isword, 而不是直接返回true;
2. for循环的时候,一定要判断cur.children[i] != null, 也就是判断存入的单词,是否这条路径;搜索所有的路径,那就是DFS搜索,每一种情况都要check,只要有一种情况是true,那么就返回true;)
思路:这道题如果做过之前的那道
Implement Trie (Prefix Tree) 实现字典树(前缀树)的话就没有太大的难度了,因为这道题里面'.'可以代替任意字符,所以一旦有了'.',就需要查找之前存下的所有下一层的不是null的path;String match 的题,一般都是DFS 参数里面加入index,然后递归 subproblem求解;
class WordDictionary {
private class TrieNode {
public TrieNode[] children;
public boolean isword;
public String word;
public TrieNode() {
this.children = new TrieNode[26];
this.isword = false;
this.word = null;
}
}
private class Trie {
public TrieNode root;
public Trie() {
this.root = new TrieNode();
}
public void insert(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(cur.children[c - 'a'] == null) {
cur.children[c - 'a'] = new TrieNode();
}
cur = cur.children[c - 'a'];
}
cur.isword = true;
cur.word = word;
}
public boolean search(String word) {
return ismatch(word, 0, root);
}
private boolean ismatch(String word, int index, TrieNode cur) {
if(index == word.length()) {
return cur.isword;
}
char c = word.charAt(index);
if(c == '.') {
for(int i = 0; i < 26; i++) {
if(cur.children[i] != null) { // 搜索存储的,下一层所有不是null的path;
if(ismatch(word, index + 1, cur.children[i])) {
return true;
}
}
}
return false;
} else {
if(cur.children[c - 'a'] == null) {
return false;
} else {
return ismatch(word, index + 1, cur.children[c - 'a']);
}
}
}
}
/** Initialize your data structure here. */
private Trie trie;
public WordDic