forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
69 lines (58 loc) · 1.83 KB
/
Solution.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class WordDictionary {
class TrieNode {
private TrieNode[] links;
private boolean end;
public TrieNode() {
this.links = new TrieNode[26];
}
public boolean contains(char c) {
return links[c - 'a'] != null;
}
public void put(char c, TrieNode trieNode) {
links[c - 'a'] = trieNode;
}
public TrieNode get(char c) {
return links[c - 'a'];
}
}
private TrieNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!node.contains(c)) {
node.put(c, new TrieNode());
}
node = node.get(c);
}
node.end = true;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return searchHelp(word, root);
}
private boolean searchHelp(String word, TrieNode root) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if ('.' == c) {
for (int j = 0; j < node.links.length; j++) {
if (node.links[j] != null && searchHelp(word.substring(i + 1), node.links[j])) {
return true;
}
}
return false;
}
if (!node.contains(c)) {
return false;
}
node = node.get(c);
}
return node != null && node.end;
}
}