Skip to content

Commit 91c3f3e

Browse files
committed
feat: add solutions to lc problem: No.0211.Design Add and Search Words Data Structure
1 parent 2abbfcd commit 91c3f3e

File tree

4 files changed

+296
-51
lines changed

4 files changed

+296
-51
lines changed

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

+108-3
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,132 @@ wordDictionary.search("b.."); // return True
4949
<li>最多调用 <code>50000</code> 次 <code>addWord</code> 和 <code>search</code></li>
5050
</ul>
5151

52-
5352
## 解法
5453

5554
<!-- 这里可写通用的实现逻辑 -->
5655

56+
“前缀树”实现。
57+
5758
<!-- tabs:start -->
5859

5960
### **Python3**
6061

6162
<!-- 这里可写当前语言的特殊实现逻辑 -->
6263

6364
```python
64-
65+
class Trie:
66+
67+
def __init__(self):
68+
self.children = [None] * 26
69+
self.is_end = False
70+
71+
72+
class WordDictionary:
73+
74+
def __init__(self):
75+
"""
76+
Initialize your data structure here.
77+
"""
78+
self.trie = Trie()
79+
80+
def addWord(self, word: str) -> None:
81+
node = self.trie
82+
for c in word:
83+
index = ord(c) - ord('a')
84+
if node.children[index] is None:
85+
node.children[index] = Trie()
86+
node = node.children[index]
87+
node.is_end = True
88+
89+
def search(self, word: str) -> bool:
90+
return self._search(word, self.trie)
91+
92+
def _search(self, word: str, node: Trie) -> bool:
93+
for i in range(len(word)):
94+
c = word[i]
95+
index = ord(c) - ord('a')
96+
if c != '.' and node.children[index] is None:
97+
return False
98+
if c == '.':
99+
for j in range(26):
100+
if node.children[j] is not None and self._search(word[i + 1:], node.children[j]):
101+
return True
102+
return False
103+
node = node.children[index]
104+
return node.is_end
105+
106+
# Your WordDictionary object will be instantiated and called as such:
107+
# obj = WordDictionary()
108+
# obj.addWord(word)
109+
# param_2 = obj.search(word)
65110
```
66111

67112
### **Java**
68113

69114
<!-- 这里可写当前语言的特殊实现逻辑 -->
70115

71116
```java
72-
117+
class WordDictionary {
118+
class Trie {
119+
Trie[] children;
120+
boolean isEnd;
121+
Trie() {
122+
children = new Trie[26];
123+
isEnd = false;
124+
}
125+
}
126+
127+
private Trie trie;
128+
129+
/** Initialize your data structure here. */
130+
public WordDictionary() {
131+
trie = new Trie();
132+
}
133+
134+
public void addWord(String word) {
135+
Trie node = trie;
136+
for (int i = 0; i < word.length(); ++i) {
137+
char c = word.charAt(i);
138+
int index = c - 'a';
139+
if (node.children[index] == null) {
140+
node.children[index] = new Trie();
141+
}
142+
node = node.children[index];
143+
}
144+
node.isEnd = true;
145+
}
146+
147+
public boolean search(String word) {
148+
return search(word, trie);
149+
}
150+
151+
private boolean search(String word, Trie node) {
152+
for (int i = 0; i < word.length(); ++i) {
153+
char c = word.charAt(i);
154+
int index = c - 'a';
155+
if (c != '.' && node.children[index] == null) {
156+
return false;
157+
}
158+
if (c == '.') {
159+
for (int j = 0; j < 26; ++j) {
160+
if (node.children[j] != null && search(word.substring(i + 1), node.children[j])) {
161+
return true;
162+
}
163+
}
164+
return false;
165+
}
166+
node = node.children[index];
167+
}
168+
return node.isEnd;
169+
}
170+
}
171+
172+
/**
173+
* Your WordDictionary object will be instantiated and called as such:
174+
* WordDictionary obj = new WordDictionary();
175+
* obj.addWord(word);
176+
* boolean param_2 = obj.search(word);
177+
*/
73178
```
74179

75180
### **...**

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

+106-3
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,124 @@ wordDictionary.search(&quot;b..&quot;); // return True
4545
<li>At most <code>50000</code>&nbsp;calls will be made to <code>addWord</code>&nbsp;and <code>search</code>.</li>
4646
</ul>
4747

48-
4948
## Solutions
5049

5150
<!-- tabs:start -->
5251

5352
### **Python3**
5453

5554
```python
56-
55+
class Trie:
56+
57+
def __init__(self):
58+
self.children = [None] * 26
59+
self.is_end = False
60+
61+
62+
class WordDictionary:
63+
64+
def __init__(self):
65+
"""
66+
Initialize your data structure here.
67+
"""
68+
self.trie = Trie()
69+
70+
def addWord(self, word: str) -> None:
71+
node = self.trie
72+
for c in word:
73+
index = ord(c) - ord('a')
74+
if node.children[index] is None:
75+
node.children[index] = Trie()
76+
node = node.children[index]
77+
node.is_end = True
78+
79+
def search(self, word: str) -> bool:
80+
return self._search(word, self.trie)
81+
82+
def _search(self, word: str, node: Trie) -> bool:
83+
for i in range(len(word)):
84+
c = word[i]
85+
index = ord(c) - ord('a')
86+
if c != '.' and node.children[index] is None:
87+
return False
88+
if c == '.':
89+
for j in range(26):
90+
if node.children[j] is not None and self._search(word[i + 1:], node.children[j]):
91+
return True
92+
return False
93+
node = node.children[index]
94+
return node.is_end
95+
96+
# Your WordDictionary object will be instantiated and called as such:
97+
# obj = WordDictionary()
98+
# obj.addWord(word)
99+
# param_2 = obj.search(word)
57100
```
58101

59102
### **Java**
60103

61104
```java
62-
105+
class WordDictionary {
106+
class Trie {
107+
Trie[] children;
108+
boolean isEnd;
109+
Trie() {
110+
children = new Trie[26];
111+
isEnd = false;
112+
}
113+
}
114+
115+
private Trie trie;
116+
117+
/** Initialize your data structure here. */
118+
public WordDictionary() {
119+
trie = new Trie();
120+
}
121+
122+
public void addWord(String word) {
123+
Trie node = trie;
124+
for (int i = 0; i < word.length(); ++i) {
125+
char c = word.charAt(i);
126+
int index = c - 'a';
127+
if (node.children[index] == null) {
128+
node.children[index] = new Trie();
129+
}
130+
node = node.children[index];
131+
}
132+
node.isEnd = true;
133+
}
134+
135+
public boolean search(String word) {
136+
return search(word, trie);
137+
}
138+
139+
private boolean search(String word, Trie node) {
140+
for (int i = 0; i < word.length(); ++i) {
141+
char c = word.charAt(i);
142+
int index = c - 'a';
143+
if (c != '.' && node.children[index] == null) {
144+
return false;
145+
}
146+
if (c == '.') {
147+
for (int j = 0; j < 26; ++j) {
148+
if (node.children[j] != null && search(word.substring(i + 1), node.children[j])) {
149+
return true;
150+
}
151+
}
152+
return false;
153+
}
154+
node = node.children[index];
155+
}
156+
return node.isEnd;
157+
}
158+
}
159+
160+
/**
161+
* Your WordDictionary object will be instantiated and called as such:
162+
* WordDictionary obj = new WordDictionary();
163+
* obj.addWord(word);
164+
* boolean param_2 = obj.search(word);
165+
*/
63166
```
64167

65168
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,61 @@
11
class WordDictionary {
2-
3-
class TrieNode {
4-
private TrieNode[] links;
5-
private boolean end;
6-
7-
public TrieNode() {
8-
this.links = new TrieNode[26];
9-
}
10-
11-
public boolean contains(char c) {
12-
return links[c - 'a'] != null;
13-
}
14-
15-
public void put(char c, TrieNode trieNode) {
16-
links[c - 'a'] = trieNode;
17-
}
18-
19-
public TrieNode get(char c) {
20-
return links[c - 'a'];
2+
class Trie {
3+
Trie[] children;
4+
boolean isEnd;
5+
Trie() {
6+
children = new Trie[26];
7+
isEnd = false;
218
}
229
}
2310

24-
private TrieNode root;
11+
private Trie trie;
2512

2613
/** Initialize your data structure here. */
2714
public WordDictionary() {
28-
root = new TrieNode();
15+
trie = new Trie();
2916
}
30-
31-
/** Adds a word into the data structure. */
17+
3218
public void addWord(String word) {
33-
TrieNode node = root;
34-
for (int i = 0; i < word.length(); i++) {
19+
Trie node = trie;
20+
for (int i = 0; i < word.length(); ++i) {
3521
char c = word.charAt(i);
36-
if (!node.contains(c)) {
37-
node.put(c, new TrieNode());
22+
int index = c - 'a';
23+
if (node.children[index] == null) {
24+
node.children[index] = new Trie();
3825
}
39-
node = node.get(c);
26+
node = node.children[index];
4027
}
41-
node.end = true;
28+
node.isEnd = true;
4229
}
43-
44-
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
30+
4531
public boolean search(String word) {
46-
return searchHelp(word, root);
32+
return search(word, trie);
4733
}
4834

49-
private boolean searchHelp(String word, TrieNode root) {
50-
TrieNode node = root;
51-
for (int i = 0; i < word.length(); i++) {
35+
private boolean search(String word, Trie node) {
36+
for (int i = 0; i < word.length(); ++i) {
5237
char c = word.charAt(i);
53-
54-
if ('.' == c) {
55-
for (int j = 0; j < node.links.length; j++) {
56-
if (node.links[j] != null && searchHelp(word.substring(i + 1), node.links[j])) {
38+
int index = c - 'a';
39+
if (c != '.' && node.children[index] == null) {
40+
return false;
41+
}
42+
if (c == '.') {
43+
for (int j = 0; j < 26; ++j) {
44+
if (node.children[j] != null && search(word.substring(i + 1), node.children[j])) {
5745
return true;
5846
}
5947
}
6048
return false;
6149
}
62-
if (!node.contains(c)) {
63-
return false;
64-
}
65-
node = node.get(c);
50+
node = node.children[index];
6651
}
67-
return node != null && node.end;
52+
return node.isEnd;
6853
}
69-
}
54+
}
55+
56+
/**
57+
* Your WordDictionary object will be instantiated and called as such:
58+
* WordDictionary obj = new WordDictionary();
59+
* obj.addWord(word);
60+
* boolean param_2 = obj.search(word);
61+
*/

0 commit comments

Comments
 (0)