14
public WordDictionary() {18
public void addWord(String word) {20
for (char c : word.toCharArray()) {22
if (temp.sub[index] == null) temp.sub[index] = new Node();23
temp = temp.sub[index];28
public boolean search(String word) {30
return dfs(temp, word, 0);33
private boolean dfs(Node node, String word, int i) {34
if (i == word.length()) return node.last;36
int index = word.charAt(i) - 'a';37
if (word.charAt(i) == '.') {38
for (int j = 0; j < 26; j++) {39
if (node.sub[j] != null) if (dfs(node.sub[j], word, i + 1)) return true;42
} else if (node.sub[index] == null) return false;43
return dfs(node.sub[index], word, i + 1);