-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddAndSearchWord.java
More file actions
65 lines (58 loc) · 1.89 KB
/
AddAndSearchWord.java
File metadata and controls
65 lines (58 loc) · 1.89 KB
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
public class WordDictionary {
private class TrieNode {
static final int R = 26;
boolean isWord;
final TrieNode[] next;
TrieNode() {
isWord = false;
next = new TrieNode[R];
}
}
private final TrieNode root = new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode curNode = root;
for(int i = 0; i < word.length(); ++i) {
char c = Character.toLowerCase(word.charAt(i));
if (c < 'a' || c > 'z') {
return;
}
TrieNode nextNode = curNode.next[c - 'a'];
if (nextNode == null) {
nextNode = new TrieNode();
curNode.next[c - 'a'] = nextNode;
}
curNode = nextNode;
}
curNode.isWord = true;
}
private boolean findNode(TrieNode node, String word, int d) {
if (node == null) {
return false;
}
if (d == word.length()) {
return node.isWord;
}
char c = word.charAt(d);
if (c != '.') {
return findNode(node.next[c - 'a'], word, d+1);
} else {
for(int i = 0; i < TrieNode.R; ++i) {
if (findNode(node.next[i], word, d+1)) {
return true;
}
}
}
return false;
}
// Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
// Can also be implemented iteratively using a stack, not hard
public boolean search(String word) {
return findNode(root, word, 0);
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");