Skip to content

Commit 9525eaa

Browse files
committed
feat: update solutions to lc problem: No.0211
No.0211.Design Add and Search Words Data Structure
1 parent 6ab3755 commit 9525eaa

File tree

4 files changed

+106
-130
lines changed

4 files changed

+106
-130
lines changed

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

+36-44
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,33 @@ class Trie:
7272
class WordDictionary:
7373

7474
def __init__(self):
75-
"""
76-
Initialize your data structure here.
77-
"""
7875
self.trie = Trie()
7976

8077
def addWord(self, word: str) -> None:
8178
node = self.trie
8279
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]
80+
idx = ord(c) - ord('a')
81+
if node.children[idx] is None:
82+
node.children[idx] = Trie()
83+
node = node.children[idx]
8784
node.is_end = True
8885

8986
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
87+
def search(word, node):
88+
for i in range(len(word)):
89+
c = word[i]
90+
idx = ord(c) - ord('a')
91+
if c != '.' and node.children[idx] is None:
92+
return False
93+
if c == '.':
94+
for child in node.children:
95+
if child is not None and search(word[i + 1:], child):
96+
return True
97+
return False
98+
node = node.children[idx]
99+
return node.is_end
100+
101+
return search(word, self.trie)
105102

106103
# Your WordDictionary object will be instantiated and called as such:
107104
# obj = WordDictionary()
@@ -114,56 +111,51 @@ class WordDictionary:
114111
<!-- 这里可写当前语言的特殊实现逻辑 -->
115112

116113
```java
117-
class WordDictionary {
118-
class Trie {
119-
Trie[] children;
120-
boolean isEnd;
121-
Trie() {
122-
children = new Trie[26];
123-
isEnd = false;
124-
}
125-
}
114+
class Trie {
115+
Trie[] children = new Trie[26];
116+
boolean isEnd;
117+
}
126118

119+
class WordDictionary {
127120
private Trie trie;
128121

129122
/** Initialize your data structure here. */
130123
public WordDictionary() {
131124
trie = new Trie();
132125
}
133-
126+
134127
public void addWord(String word) {
135128
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();
129+
for (char c : word.toCharArray()) {
130+
int idx = c - 'a';
131+
if (node.children[idx] == null) {
132+
node.children[idx] = new Trie();
141133
}
142-
node = node.children[index];
134+
node = node.children[idx];
143135
}
144136
node.isEnd = true;
145137
}
146-
138+
147139
public boolean search(String word) {
148140
return search(word, trie);
149141
}
150142

151143
private boolean search(String word, Trie node) {
152144
for (int i = 0; i < word.length(); ++i) {
153145
char c = word.charAt(i);
154-
int index = c - 'a';
155-
if (c != '.' && node.children[index] == null) {
146+
int idx = c - 'a';
147+
if (c != '.' && node.children[idx] == null) {
156148
return false;
157149
}
158150
if (c == '.') {
159-
for (int j = 0; j < 26; ++j) {
160-
if (node.children[j] != null && search(word.substring(i + 1), node.children[j])) {
151+
for (Trie child : node.children) {
152+
if (child != null && search(word.substring(i + 1), child)) {
161153
return true;
162154
}
163155
}
164156
return false;
165157
}
166-
node = node.children[index];
158+
node = node.children[idx];
167159
}
168160
return node.isEnd;
169161
}

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

+36-44
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,33 @@ class Trie:
6262
class WordDictionary:
6363

6464
def __init__(self):
65-
"""
66-
Initialize your data structure here.
67-
"""
6865
self.trie = Trie()
6966

7067
def addWord(self, word: str) -> None:
7168
node = self.trie
7269
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]
70+
idx = ord(c) - ord('a')
71+
if node.children[idx] is None:
72+
node.children[idx] = Trie()
73+
node = node.children[idx]
7774
node.is_end = True
7875

7976
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
77+
def search(word, node):
78+
for i in range(len(word)):
79+
c = word[i]
80+
idx = ord(c) - ord('a')
81+
if c != '.' and node.children[idx] is None:
82+
return False
83+
if c == '.':
84+
for child in node.children:
85+
if child is not None and search(word[i + 1:], child):
86+
return True
87+
return False
88+
node = node.children[idx]
89+
return node.is_end
90+
91+
return search(word, self.trie)
9592

9693
# Your WordDictionary object will be instantiated and called as such:
9794
# obj = WordDictionary()
@@ -102,56 +99,51 @@ class WordDictionary:
10299
### **Java**
103100

104101
```java
105-
class WordDictionary {
106-
class Trie {
107-
Trie[] children;
108-
boolean isEnd;
109-
Trie() {
110-
children = new Trie[26];
111-
isEnd = false;
112-
}
113-
}
102+
class Trie {
103+
Trie[] children = new Trie[26];
104+
boolean isEnd;
105+
}
114106

107+
class WordDictionary {
115108
private Trie trie;
116109

117110
/** Initialize your data structure here. */
118111
public WordDictionary() {
119112
trie = new Trie();
120113
}
121-
114+
122115
public void addWord(String word) {
123116
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();
117+
for (char c : word.toCharArray()) {
118+
int idx = c - 'a';
119+
if (node.children[idx] == null) {
120+
node.children[idx] = new Trie();
129121
}
130-
node = node.children[index];
122+
node = node.children[idx];
131123
}
132124
node.isEnd = true;
133125
}
134-
126+
135127
public boolean search(String word) {
136128
return search(word, trie);
137129
}
138130

139131
private boolean search(String word, Trie node) {
140132
for (int i = 0; i < word.length(); ++i) {
141133
char c = word.charAt(i);
142-
int index = c - 'a';
143-
if (c != '.' && node.children[index] == null) {
134+
int idx = c - 'a';
135+
if (c != '.' && node.children[idx] == null) {
144136
return false;
145137
}
146138
if (c == '.') {
147-
for (int j = 0; j < 26; ++j) {
148-
if (node.children[j] != null && search(word.substring(i + 1), node.children[j])) {
139+
for (Trie child : node.children) {
140+
if (child != null && search(word.substring(i + 1), child)) {
149141
return true;
150142
}
151143
}
152144
return false;
153145
}
154-
node = node.children[index];
146+
node = node.children[idx];
155147
}
156148
return node.isEnd;
157149
}

solution/0200-0299/0211.Design Add and Search Words Data Structure/Solution.java

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
class WordDictionary {
2-
class Trie {
3-
Trie[] children;
4-
boolean isEnd;
5-
Trie() {
6-
children = new Trie[26];
7-
isEnd = false;
8-
}
9-
}
1+
class Trie {
2+
Trie[] children = new Trie[26];
3+
boolean isEnd;
4+
}
105

6+
class WordDictionary {
117
private Trie trie;
128

139
/** Initialize your data structure here. */
@@ -17,13 +13,12 @@ public WordDictionary() {
1713

1814
public void addWord(String word) {
1915
Trie node = trie;
20-
for (int i = 0; i < word.length(); ++i) {
21-
char c = word.charAt(i);
22-
int index = c - 'a';
23-
if (node.children[index] == null) {
24-
node.children[index] = new Trie();
16+
for (char c : word.toCharArray()) {
17+
int idx = c - 'a';
18+
if (node.children[idx] == null) {
19+
node.children[idx] = new Trie();
2520
}
26-
node = node.children[index];
21+
node = node.children[idx];
2722
}
2823
node.isEnd = true;
2924
}
@@ -35,19 +30,19 @@ public boolean search(String word) {
3530
private boolean search(String word, Trie node) {
3631
for (int i = 0; i < word.length(); ++i) {
3732
char c = word.charAt(i);
38-
int index = c - 'a';
39-
if (c != '.' && node.children[index] == null) {
33+
int idx = c - 'a';
34+
if (c != '.' && node.children[idx] == null) {
4035
return false;
4136
}
4237
if (c == '.') {
43-
for (int j = 0; j < 26; ++j) {
44-
if (node.children[j] != null && search(word.substring(i + 1), node.children[j])) {
38+
for (Trie child : node.children) {
39+
if (child != null && search(word.substring(i + 1), child)) {
4540
return true;
4641
}
4742
}
4843
return false;
4944
}
50-
node = node.children[index];
45+
node = node.children[idx];
5146
}
5247
return node.isEnd;
5348
}

solution/0200-0299/0211.Design Add and Search Words Data Structure/Solution.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,33 @@ def __init__(self):
88
class WordDictionary:
99

1010
def __init__(self):
11-
"""
12-
Initialize your data structure here.
13-
"""
1411
self.trie = Trie()
1512

1613
def addWord(self, word: str) -> None:
1714
node = self.trie
1815
for c in word:
19-
index = ord(c) - ord('a')
20-
if node.children[index] is None:
21-
node.children[index] = Trie()
22-
node = node.children[index]
16+
idx = ord(c) - ord('a')
17+
if node.children[idx] is None:
18+
node.children[idx] = Trie()
19+
node = node.children[idx]
2320
node.is_end = True
2421

2522
def search(self, word: str) -> bool:
26-
return self._search(word, self.trie)
27-
28-
def _search(self, word: str, node: Trie) -> bool:
29-
for i in range(len(word)):
30-
c = word[i]
31-
index = ord(c) - ord('a')
32-
if c != '.' and node.children[index] is None:
33-
return False
34-
if c == '.':
35-
for j in range(26):
36-
if node.children[j] is not None and self._search(word[i + 1:], node.children[j]):
37-
return True
38-
return False
39-
node = node.children[index]
40-
return node.is_end
23+
def search(word, node):
24+
for i in range(len(word)):
25+
c = word[i]
26+
idx = ord(c) - ord('a')
27+
if c != '.' and node.children[idx] is None:
28+
return False
29+
if c == '.':
30+
for child in node.children:
31+
if child is not None and search(word[i + 1:], child):
32+
return True
33+
return False
34+
node = node.children[idx]
35+
return node.is_end
36+
37+
return search(word, self.trie)
4138

4239
# Your WordDictionary object will be instantiated and called as such:
4340
# obj = WordDictionary()

0 commit comments

Comments
 (0)