-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution211.java
50 lines (43 loc) · 1.31 KB
/
Solution211.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 添加与搜索单词 - 数据结构设计
* @date: 2019/02/14
*/
public class Solution211 {
TrieNode root = new TrieNode();
public void addWord(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
if (node.children[c - 'a'] == null) {
node.children[c - 'a'] = new TrieNode();
}
node = node.children[c - 'a'];
}
node.item = word;
}
public boolean search(String word) {
return dfs(root, word.toCharArray(), 0);
}
private boolean dfs(TrieNode node, char[] chs, int index) {
if (index == chs.length) {
return !node.item.equals("");
}
if ('.' != chs[index]) {
return null != node.children[chs[index] - 'a'] && dfs(node.children[chs[index] - 'a'], chs, index + 1);
} else {
for (int i = 0; i < node.children.length; i++) {
if (node.children[i] != null) {
if (dfs(node.children[i], chs, index + 1)) {
return true;
}
}
}
}
return false;
}
private class TrieNode {
TrieNode[] children = new TrieNode[26];
String item = "";
}
}