File tree 3 files changed +184
-0
lines changed
solution/0200-0299/0208.Implement Trie (Prefix Tree)
3 files changed +184
-0
lines changed Original file line number Diff line number Diff line change @@ -200,6 +200,69 @@ class Trie {
200
200
*/
201
201
```
202
202
203
+ ### ** JavaScript**
204
+
205
+ ``` js
206
+ /**
207
+ * Initialize your data structure here.
208
+ */
209
+ var Trie = function () {
210
+ this .children = {};
211
+ };
212
+
213
+ /**
214
+ * Inserts a word into the trie.
215
+ * @param {string} word
216
+ * @return {void}
217
+ */
218
+ Trie .prototype .insert = function (word ) {
219
+ let node = this .children ;
220
+ for (let char of word) {
221
+ if (! node[char]) {
222
+ node[char] = {};
223
+ }
224
+ node = node[char];
225
+ }
226
+ node .isEnd = true ;
227
+ };
228
+
229
+ /**
230
+ * Returns if the word is in the trie.
231
+ * @param {string} word
232
+ * @return {boolean}
233
+ */
234
+ Trie .prototype .search = function (word ) {
235
+ let node = this .searchPrefix (word);
236
+ return node != undefined && node .isEnd != undefined ;
237
+ };
238
+
239
+ Trie .prototype .searchPrefix = function (prefix ) {
240
+ let node = this .children ;
241
+ for (let char of prefix) {
242
+ if (! node[char]) return false ;
243
+ node = node[char];
244
+ }
245
+ return node;
246
+ }
247
+
248
+ /**
249
+ * Returns if there is any word in the trie that starts with the given prefix.
250
+ * @param {string} prefix
251
+ * @return {boolean}
252
+ */
253
+ Trie .prototype .startsWith = function (prefix ) {
254
+ return this .searchPrefix (prefix);
255
+ };
256
+
257
+ /**
258
+ * Your Trie object will be instantiated and called as such:
259
+ * var obj = new Trie()
260
+ * obj.insert(word)
261
+ * var param_2 = obj.search(word)
262
+ * var param_3 = obj.startsWith(prefix)
263
+ */
264
+ ```
265
+
203
266
### ** ...**
204
267
205
268
```
Original file line number Diff line number Diff line change @@ -165,6 +165,69 @@ class Trie {
165
165
*/
166
166
```
167
167
168
+ ### ** JavaScript**
169
+
170
+ ``` js
171
+ /**
172
+ * Initialize your data structure here.
173
+ */
174
+ var Trie = function () {
175
+ this .children = {};
176
+ };
177
+
178
+ /**
179
+ * Inserts a word into the trie.
180
+ * @param {string} word
181
+ * @return {void}
182
+ */
183
+ Trie .prototype .insert = function (word ) {
184
+ let node = this .children ;
185
+ for (let char of word) {
186
+ if (! node[char]) {
187
+ node[char] = {};
188
+ }
189
+ node = node[char];
190
+ }
191
+ node .isEnd = true ;
192
+ };
193
+
194
+ /**
195
+ * Returns if the word is in the trie.
196
+ * @param {string} word
197
+ * @return {boolean}
198
+ */
199
+ Trie .prototype .search = function (word ) {
200
+ let node = this .searchPrefix (word);
201
+ return node != undefined && node .isEnd != undefined ;
202
+ };
203
+
204
+ Trie .prototype .searchPrefix = function (prefix ) {
205
+ let node = this .children ;
206
+ for (let char of prefix) {
207
+ if (! node[char]) return false ;
208
+ node = node[char];
209
+ }
210
+ return node;
211
+ }
212
+
213
+ /**
214
+ * Returns if there is any word in the trie that starts with the given prefix.
215
+ * @param {string} prefix
216
+ * @return {boolean}
217
+ */
218
+ Trie .prototype .startsWith = function (prefix ) {
219
+ return this .searchPrefix (prefix);
220
+ };
221
+
222
+ /**
223
+ * Your Trie object will be instantiated and called as such:
224
+ * var obj = new Trie()
225
+ * obj.insert(word)
226
+ * var param_2 = obj.search(word)
227
+ * var param_3 = obj.startsWith(prefix)
228
+ */
229
+ ```
230
+
168
231
### ** ...**
169
232
170
233
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Initialize your data structure here.
3
+ */
4
+ var Trie = function ( ) {
5
+ this . children = { } ;
6
+ } ;
7
+
8
+ /**
9
+ * Inserts a word into the trie.
10
+ * @param {string } word
11
+ * @return {void }
12
+ */
13
+ Trie . prototype . insert = function ( word ) {
14
+ let node = this . children ;
15
+ for ( let char of word ) {
16
+ if ( ! node [ char ] ) {
17
+ node [ char ] = { } ;
18
+ }
19
+ node = node [ char ] ;
20
+ }
21
+ node . isEnd = true ;
22
+ } ;
23
+
24
+ /**
25
+ * Returns if the word is in the trie.
26
+ * @param {string } word
27
+ * @return {boolean }
28
+ */
29
+ Trie . prototype . search = function ( word ) {
30
+ let node = this . searchPrefix ( word ) ;
31
+ return node != undefined && node . isEnd != undefined ;
32
+ } ;
33
+
34
+ Trie . prototype . searchPrefix = function ( prefix ) {
35
+ let node = this . children ;
36
+ for ( let char of prefix ) {
37
+ if ( ! node [ char ] ) return false ;
38
+ node = node [ char ] ;
39
+ }
40
+ return node ;
41
+ }
42
+
43
+ /**
44
+ * Returns if there is any word in the trie that starts with the given prefix.
45
+ * @param {string } prefix
46
+ * @return {boolean }
47
+ */
48
+ Trie . prototype . startsWith = function ( prefix ) {
49
+ return this . searchPrefix ( prefix ) ;
50
+ } ;
51
+
52
+ /**
53
+ * Your Trie object will be instantiated and called as such:
54
+ * var obj = new Trie()
55
+ * obj.insert(word)
56
+ * var param_2 = obj.search(word)
57
+ * var param_3 = obj.startsWith(prefix)
58
+ */
You can’t perform that action at this time.
0 commit comments