-
Notifications
You must be signed in to change notification settings - Fork 7
/
add-and-search-word-data-structure-design.java
63 lines (56 loc) · 1.94 KB
/
add-and-search-word-data-structure-design.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
public class WordDictionary {
public class TrieNode {
TrieNode links[];
boolean isWord = false;
public TrieNode() {
links = new TrieNode[26];
for (int i = 0; i < 26; i++) links[i] = null;
}
}
TrieNode head = new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode cur = head;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (cur.links[index] == null) {
cur.links[index] = new TrieNode();
}
cur = cur.links[index];
}
cur.isWord = 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 searchFrom(word, head);
}
public boolean searchFrom(String word, TrieNode root) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
// System.out.println(word.charAt(i));
if (cur != null) {
if (word.charAt(i) == '.') {
for (int j = 0; j < 26; j++) {
// System.out.println(j + " " + cur.links[j]);
if (searchFrom(word.substring(i+1), cur.links[j]))
return true;
}
cur = null;
} else {
int index = word.charAt(i) - 'a';
// if (cur.links[index] != null) {
cur = cur.links[index];
// }
}
} else {
break;
}
}
return cur != null && cur.isWord;
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");