Skip to content

Commit 9cee755

Browse files
committed
feat: add go,cpp solutions to lc problem: No.0211
No.0211.Design Add and Search Words Data Structure
1 parent 28de2ff commit 9cee755

File tree

4 files changed

+404
-0
lines changed

4 files changed

+404
-0
lines changed

solution/0200-0299/0211.Design Add and Search Words Data Structure/README.md

+138
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,144 @@ class WordDictionary {
177177
*/
178178
```
179179

180+
### **Go**
181+
182+
```go
183+
type WordDictionary struct {
184+
root *trie
185+
}
186+
187+
func Constructor() WordDictionary {
188+
return WordDictionary{new(trie)}
189+
}
190+
191+
func (this *WordDictionary) AddWord(word string) {
192+
this.root.insert(word)
193+
}
194+
195+
func (this *WordDictionary) Search(word string) bool {
196+
n := len(word)
197+
198+
var dfs func(int, *trie) bool
199+
dfs = func(i int, cur *trie) bool {
200+
if i == n {
201+
return cur.isEnd
202+
}
203+
c := word[i]
204+
if c != '.' {
205+
child := cur.children[c-'a']
206+
if child != nil && dfs(i+1, child) {
207+
return true
208+
}
209+
} else {
210+
for _, child := range cur.children {
211+
if child != nil && dfs(i+1, child) {
212+
return true
213+
}
214+
}
215+
}
216+
return false
217+
}
218+
219+
return dfs(0, this.root)
220+
}
221+
222+
type trie struct {
223+
children [26]*trie
224+
isEnd bool
225+
}
226+
227+
func (t *trie) insert(word string) {
228+
cur := t
229+
for _, c := range word {
230+
c -= 'a'
231+
if cur.children[c] == nil {
232+
cur.children[c] = new(trie)
233+
}
234+
cur = cur.children[c]
235+
}
236+
cur.isEnd = true
237+
}
238+
239+
/**
240+
* Your WordDictionary object will be instantiated and called as such:
241+
* obj := Constructor();
242+
* obj.AddWord(word);
243+
* param_2 := obj.Search(word);
244+
*/
245+
```
246+
247+
### **C++**
248+
249+
```cpp
250+
class trie {
251+
public:
252+
vector<trie*> children;
253+
bool is_end;
254+
255+
trie() {
256+
children = vector<trie*>(26, nullptr);
257+
is_end = false;
258+
}
259+
260+
void insert(const string& word) {
261+
trie* cur = this;
262+
for (char c : word) {
263+
c -= 'a';
264+
if (cur->children[c] == nullptr) {
265+
cur->children[c] = new trie;
266+
}
267+
cur = cur->children[c];
268+
}
269+
cur->is_end = true;
270+
}
271+
};
272+
273+
class WordDictionary {
274+
private:
275+
trie* root;
276+
277+
public:
278+
WordDictionary() : root(new trie) {}
279+
280+
void addWord(string word) {
281+
root->insert(word);
282+
}
283+
284+
bool search(string word) {
285+
return dfs(word, 0, root);
286+
}
287+
288+
private:
289+
bool dfs(const string& word, int i, trie* cur) {
290+
if (i == word.size()) {
291+
return cur->is_end;
292+
}
293+
char c = word[i];
294+
if (c != '.') {
295+
trie* child = cur->children[c - 'a'];
296+
if (child != nullptr && dfs(word, i + 1, child)) {
297+
return true;
298+
}
299+
} else {
300+
for (trie* child : cur->children) {
301+
if (child != nullptr && dfs(word, i + 1, child)) {
302+
return true;
303+
}
304+
}
305+
}
306+
return false;
307+
}
308+
};
309+
310+
/**
311+
* Your WordDictionary object will be instantiated and called as such:
312+
* WordDictionary* obj = new WordDictionary();
313+
* obj->addWord(word);
314+
* bool param_2 = obj->search(word);
315+
*/
316+
```
317+
180318
### **...**
181319
182320
```

solution/0200-0299/0211.Design Add and Search Words Data Structure/README_EN.md

+138
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,144 @@ class WordDictionary {
165165
*/
166166
```
167167

168+
### **Go**
169+
170+
```go
171+
type WordDictionary struct {
172+
root *trie
173+
}
174+
175+
func Constructor() WordDictionary {
176+
return WordDictionary{new(trie)}
177+
}
178+
179+
func (this *WordDictionary) AddWord(word string) {
180+
this.root.insert(word)
181+
}
182+
183+
func (this *WordDictionary) Search(word string) bool {
184+
n := len(word)
185+
186+
var dfs func(int, *trie) bool
187+
dfs = func(i int, cur *trie) bool {
188+
if i == n {
189+
return cur.isEnd
190+
}
191+
c := word[i]
192+
if c != '.' {
193+
child := cur.children[c-'a']
194+
if child != nil && dfs(i+1, child) {
195+
return true
196+
}
197+
} else {
198+
for _, child := range cur.children {
199+
if child != nil && dfs(i+1, child) {
200+
return true
201+
}
202+
}
203+
}
204+
return false
205+
}
206+
207+
return dfs(0, this.root)
208+
}
209+
210+
type trie struct {
211+
children [26]*trie
212+
isEnd bool
213+
}
214+
215+
func (t *trie) insert(word string) {
216+
cur := t
217+
for _, c := range word {
218+
c -= 'a'
219+
if cur.children[c] == nil {
220+
cur.children[c] = new(trie)
221+
}
222+
cur = cur.children[c]
223+
}
224+
cur.isEnd = true
225+
}
226+
227+
/**
228+
* Your WordDictionary object will be instantiated and called as such:
229+
* obj := Constructor();
230+
* obj.AddWord(word);
231+
* param_2 := obj.Search(word);
232+
*/
233+
```
234+
235+
### **C++**
236+
237+
```cpp
238+
class trie {
239+
public:
240+
vector<trie*> children;
241+
bool is_end;
242+
243+
trie() {
244+
children = vector<trie*>(26, nullptr);
245+
is_end = false;
246+
}
247+
248+
void insert(const string& word) {
249+
trie* cur = this;
250+
for (char c : word) {
251+
c -= 'a';
252+
if (cur->children[c] == nullptr) {
253+
cur->children[c] = new trie;
254+
}
255+
cur = cur->children[c];
256+
}
257+
cur->is_end = true;
258+
}
259+
};
260+
261+
class WordDictionary {
262+
private:
263+
trie* root;
264+
265+
public:
266+
WordDictionary() : root(new trie) {}
267+
268+
void addWord(string word) {
269+
root->insert(word);
270+
}
271+
272+
bool search(string word) {
273+
return dfs(word, 0, root);
274+
}
275+
276+
private:
277+
bool dfs(const string& word, int i, trie* cur) {
278+
if (i == word.size()) {
279+
return cur->is_end;
280+
}
281+
char c = word[i];
282+
if (c != '.') {
283+
trie* child = cur->children[c - 'a'];
284+
if (child != nullptr && dfs(word, i + 1, child)) {
285+
return true;
286+
}
287+
} else {
288+
for (trie* child : cur->children) {
289+
if (child != nullptr && dfs(word, i + 1, child)) {
290+
return true;
291+
}
292+
}
293+
}
294+
return false;
295+
}
296+
};
297+
298+
/**
299+
* Your WordDictionary object will be instantiated and called as such:
300+
* WordDictionary* obj = new WordDictionary();
301+
* obj->addWord(word);
302+
* bool param_2 = obj->search(word);
303+
*/
304+
```
305+
168306
### **...**
169307
170308
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class trie {
2+
public:
3+
vector<trie*> children;
4+
bool is_end;
5+
6+
trie() {
7+
children = vector<trie*>(26, nullptr);
8+
is_end = false;
9+
}
10+
11+
void insert(const string& word) {
12+
trie* cur = this;
13+
for (char c : word) {
14+
c -= 'a';
15+
if (cur->children[c] == nullptr) {
16+
cur->children[c] = new trie;
17+
}
18+
cur = cur->children[c];
19+
}
20+
cur->is_end = true;
21+
}
22+
};
23+
24+
class WordDictionary {
25+
private:
26+
trie* root;
27+
28+
public:
29+
WordDictionary() : root(new trie) {}
30+
31+
void addWord(string word) {
32+
root->insert(word);
33+
}
34+
35+
bool search(string word) {
36+
return dfs(word, 0, root);
37+
}
38+
39+
private:
40+
bool dfs(const string& word, int i, trie* cur) {
41+
if (i == word.size()) {
42+
return cur->is_end;
43+
}
44+
char c = word[i];
45+
if (c != '.') {
46+
trie* child = cur->children[c - 'a'];
47+
if (child != nullptr && dfs(word, i + 1, child)) {
48+
return true;
49+
}
50+
} else {
51+
for (trie* child : cur->children) {
52+
if (child != nullptr && dfs(word, i + 1, child)) {
53+
return true;
54+
}
55+
}
56+
}
57+
return false;
58+
}
59+
};
60+
61+
/**
62+
* Your WordDictionary object will be instantiated and called as such:
63+
* WordDictionary* obj = new WordDictionary();
64+
* obj->addWord(word);
65+
* bool param_2 = obj->search(word);
66+
*/

0 commit comments

Comments
 (0)