forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
43 lines (38 loc) · 1.37 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
class WordDictionary {
class TrieNode {
public String item = "";
public TrieNode[] children = new TrieNode[26];
}
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 cur = this.root;
for (char c : word.toCharArray()) {
if (cur.children[c - 'a'] == null) cur.children[c - 'a'] = new TrieNode();
cur = cur.children[c - 'a'];
}
cur.item = word;
}
/** 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 helper(word, 0, this.root);
}
private boolean helper (String word, int index, TrieNode root) {
if (index >= word.length()) return !root.item.equals("");
char c = word.charAt(index);
if (c == '.') {
for (int i = 0; i < 26; i++) {
if (root.children[i] != null && helper(word, index + 1, root.children[i]))
return true;
}
return false;
} else {
if (root.children[c - 'a'] == null) return false;
return helper(word, index + 1, root.children[c - 'a']);
}
}
}