-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd and Search Word - Data Structure Desgin.java
57 lines (50 loc) · 1.72 KB
/
Add and Search Word - Data Structure Desgin.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
class WordDictionary {
class TrieNode {
TrieNode[] children = new TrieNode[26];
boolean isWord = false; // mark if it's the end of a word
}
TrieNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
char[] cs = word.toCharArray();
TrieNode node = root;
for(int i = 0; i < cs.length; i++) {
char c = cs[i];
if(node.children[c-'a'] == null) {
node.children[c-'a'] = new TrieNode();
}
node = node.children[c-'a'];
}
node.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 search(word.toCharArray(), 0, root);
}
public boolean search(char[] cs, int k, TrieNode node) {
if(k == cs.length) return node.isWord;
char c = cs[k];
if(c == '.') {
for(int i = 0; i < 26; i++) {
if(node.children[i] != null) {
if(search(cs, k+1, node.children[i])) return true;
}
}
}
else {
if(node.children[c-'a'] == null) return false;
return search(cs, k+1, node.children[c-'a']);
}
return false;
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/