-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdd and Search Word - Data structure design
61 lines (49 loc) · 1.51 KB
/
Add and Search Word - Data structure design
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
class Trie{
Trie[] letters;
boolean end;
Trie(){
letters = new Trie[26];
end = false;
}
}
class WordDictionary {
/** Initialize your data structure here. */
Trie t = new Trie();
public WordDictionary() {
}
/** Adds a word into the data structure. */
public void addWord(String word) {
Trie p = t;
for(char chr : word.toCharArray()){
if(p.letters[chr - 'a'] == null)
p.letters[chr - 'a'] = new Trie();
p = p.letters[chr - 'a'];
}
p.end = true;
}
public boolean search(Trie p, String word, int index){
if(index >= word.length()) return p.end;
char c = word.charAt(index);
if(c == '.'){
for(Trie t : p.letters){
if(t != null && search(t, word, index + 1))
return true;
}
return false;
}
if(p == null || p.letters[c - 'a'] == null){
return false;
}
return search(p.letters[c - 'a'], word, index + 1);
}
/** 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(t, word, 0);
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/