-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAddandSearchWord.java
214 lines (186 loc) · 6.02 KB
/
AddandSearchWord.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Version1 use Array
class TrieNode {
public TrieNode[] children;
public boolean hasWord;
public TrieNode() {
children = new TrieNode[26];
for (int i = 0; i < 26; ++i)
children[i] = null;
hasWord = false;
}
}
public class WordDictionary {
private TrieNode root;
public WordDictionary(){
root = new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
// Write your code here
TrieNode now = root;
for(int i = 0; i < word.length(); i++) {
Character c = word.charAt(i);
if (now.children[c - 'a'] == null) {
now.children[c - 'a'] = new TrieNode();
}
now = now.children[c - 'a'];
}
now.hasWord = true;
}
boolean find(String word, int index, TrieNode now) {
if(index == word.length()) {
return now.hasWord;
}
Character c = word.charAt(index);
if (c == '.') {
for(int i = 0; i < 26; ++i)
if (now.children[i] != null) {
if (find(word, index+1, now.children[i]))
return true;
}
return false;
} else if (now.children[c - 'a'] != null) {
return find(word, index+1, now.children[c - 'a']);
} else {
return false;
}
}
// 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) {
// Write your code here
return find(word, 0, root);
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
// Version 2 use HashMap and dfs
class TrieNode {
// Initialize your data structure here.
public HashMap<Character, TrieNode> children;
public boolean hasWord;
// Initialize your data structure here.
public TrieNode() {
children = new HashMap<Character, TrieNode>();
hasWord = false;
}
}
public class WordDictionary {
private TrieNode root;
public WordDictionary(){
root = new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
// Write your code here
TrieNode now = root;
for(int i = 0; i < word.length(); i++) {
Character c = word.charAt(i);
if (!now.children.containsKey(c)) {
now.children.put(c, new TrieNode());
}
now = now.children.get(c);
}
now.hasWord = true;
}
boolean find(String word, int index, TrieNode now) {
if(index == word.length()){
if(now.children.size()==0)
return true;
else
return false;
}
Character c = word.charAt(index);
if (now.children.containsKey(c)) {
if(index == word.length()-1 && now.children.get(c).hasWord){
return true;
}
return find(word, index+1, now.children.get(c)) ;
}else if(c == '.'){
boolean result = false;
for(Map.Entry<Character, TrieNode> child: now.children.entrySet()){
if(index == word.length()-1 && child.getValue().hasWord){
return true;
}
//if any path is true, set result to be true;
if(find(word, index+1, child.getValue()) ){
result = true;
}
}
return result;
}else{
return false;
}
}
// 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) {
// Write your code here
return find(word, 0, root);
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
//version 3: use HashMap and bfs
class TrieNode{
public Map<Character,TrieNode> children;
public boolean hasWord;
public TrieNode(){
children=new HashMap<>();
hasWord=false;
}
}
public class WordDictionary {
TrieNode root;
public WordDictionary(){
root=new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
// Write your code here
TrieNode cur=root;
for(int i=0;i<word.length();++i){
char c=word.charAt(i);
TrieNode nextNode=cur.children.get(c);
if(nextNode==null){
nextNode=new TrieNode();
cur.children.put(c,nextNode);
}
cur=nextNode;
}
cur.hasWord=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) {
// Write your code here
Queue<TrieNode> nexts=new LinkedList<>();
nexts.add(root);
int index=0;
while(!nexts.isEmpty()){
int size=nexts.size();
char c=word.charAt(index);
boolean flag=false;
for(int i=0;i<size;++i){
TrieNode cur=nexts.poll();
if(c=='.'){
for(TrieNode tempNode:cur.children.values()){
nexts.add(tempNode);
flag|=tempNode.hasWord;
}
} else if(cur.children.containsKey(c)){
TrieNode nextNode=cur.children.get(c);
flag|=nextNode.hasWord;
nexts.add(nextNode);
}
}
index++;
if(index>=word.length()) return flag;
}
return false;
}
}