|
1 | 1 | --- |
2 | 2 | comments: true |
3 | 3 | difficulty: Medium |
4 | | -edit_url: https://github.com/doocs/leetcode/edit/main/solution/0200-0299/0208.Implement%20Trie%20%28Prefix%20Tree%29/README_EN.md |
| 4 | +edit_url: Antim |
5 | 5 | tags: |
6 | 6 | - Design |
7 | 7 | - Trie |
|
13 | 13 |
|
14 | 14 | # [208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) |
15 | 15 |
|
16 | | -[中文文档](/solution/0200-0299/0208.Implement%20Trie%20%28Prefix%20Tree%29/README.md) |
17 | 16 |
|
18 | 17 | ## Description |
19 | 18 |
|
@@ -219,329 +218,6 @@ public: |
219 | 218 | */ |
220 | 219 | ``` |
221 | 220 |
|
222 | | -#### Go |
223 | | -
|
224 | | -```go |
225 | | -type Trie struct { |
226 | | - children [26]*Trie |
227 | | - isEnd bool |
228 | | -} |
229 | | -
|
230 | | -func Constructor() Trie { |
231 | | - return Trie{} |
232 | | -} |
233 | | -
|
234 | | -func (this *Trie) Insert(word string) { |
235 | | - node := this |
236 | | - for _, c := range word { |
237 | | - idx := c - 'a' |
238 | | - if node.children[idx] == nil { |
239 | | - node.children[idx] = &Trie{} |
240 | | - } |
241 | | - node = node.children[idx] |
242 | | - } |
243 | | - node.isEnd = true |
244 | | -} |
245 | | -
|
246 | | -func (this *Trie) Search(word string) bool { |
247 | | - node := this.SearchPrefix(word) |
248 | | - return node != nil && node.isEnd |
249 | | -} |
250 | | -
|
251 | | -func (this *Trie) StartsWith(prefix string) bool { |
252 | | - node := this.SearchPrefix(prefix) |
253 | | - return node != nil |
254 | | -} |
255 | | -
|
256 | | -func (this *Trie) SearchPrefix(s string) *Trie { |
257 | | - node := this |
258 | | - for _, c := range s { |
259 | | - idx := c - 'a' |
260 | | - if node.children[idx] == nil { |
261 | | - return nil |
262 | | - } |
263 | | - node = node.children[idx] |
264 | | - } |
265 | | - return node |
266 | | -} |
267 | | -
|
268 | | -/** |
269 | | - * Your Trie object will be instantiated and called as such: |
270 | | - * obj := Constructor(); |
271 | | - * obj.Insert(word); |
272 | | - * param_2 := obj.Search(word); |
273 | | - * param_3 := obj.StartsWith(prefix); |
274 | | - */ |
275 | | -``` |
276 | | - |
277 | | -#### TypeScript |
278 | | - |
279 | | -```ts |
280 | | -class TrieNode { |
281 | | - children; |
282 | | - isEnd; |
283 | | - constructor() { |
284 | | - this.children = new Array(26); |
285 | | - this.isEnd = false; |
286 | | - } |
287 | | -} |
288 | | - |
289 | | -class Trie { |
290 | | - root; |
291 | | - constructor() { |
292 | | - this.root = new TrieNode(); |
293 | | - } |
294 | | - |
295 | | - insert(word: string): void { |
296 | | - let head = this.root; |
297 | | - for (let char of word) { |
298 | | - let index = char.charCodeAt(0) - 97; |
299 | | - if (!head.children[index]) { |
300 | | - head.children[index] = new TrieNode(); |
301 | | - } |
302 | | - head = head.children[index]; |
303 | | - } |
304 | | - head.isEnd = true; |
305 | | - } |
306 | | - |
307 | | - search(word: string): boolean { |
308 | | - let head = this.searchPrefix(word); |
309 | | - return head != null && head.isEnd; |
310 | | - } |
311 | | - |
312 | | - startsWith(prefix: string): boolean { |
313 | | - return this.searchPrefix(prefix) != null; |
314 | | - } |
315 | | - |
316 | | - private searchPrefix(prefix: string) { |
317 | | - let head = this.root; |
318 | | - for (let char of prefix) { |
319 | | - let index = char.charCodeAt(0) - 97; |
320 | | - if (!head.children[index]) return null; |
321 | | - head = head.children[index]; |
322 | | - } |
323 | | - return head; |
324 | | - } |
325 | | -} |
326 | | -``` |
327 | | - |
328 | | -#### Rust |
329 | | - |
330 | | -```rust |
331 | | -use std::{cell::RefCell, collections::HashMap, rc::Rc}; |
332 | | - |
333 | | -struct TrieNode { |
334 | | - pub val: Option<char>, |
335 | | - pub flag: bool, |
336 | | - pub child: HashMap<char, Rc<RefCell<TrieNode>>>, |
337 | | -} |
338 | | - |
339 | | -impl TrieNode { |
340 | | - fn new() -> Self { |
341 | | - Self { |
342 | | - val: None, |
343 | | - flag: false, |
344 | | - child: HashMap::new(), |
345 | | - } |
346 | | - } |
347 | | - |
348 | | - fn new_with_val(val: char) -> Self { |
349 | | - Self { |
350 | | - val: Some(val), |
351 | | - flag: false, |
352 | | - child: HashMap::new(), |
353 | | - } |
354 | | - } |
355 | | -} |
356 | | - |
357 | | -struct Trie { |
358 | | - root: Rc<RefCell<TrieNode>>, |
359 | | -} |
360 | | - |
361 | | -/// Your Trie object will be instantiated and called as such: |
362 | | -/// let obj = Trie::new(); |
363 | | -/// obj.insert(word); |
364 | | -/// let ret_2: bool = obj.search(word); |
365 | | -/// let ret_3: bool = obj.starts_with(prefix); |
366 | | -impl Trie { |
367 | | - fn new() -> Self { |
368 | | - Self { |
369 | | - root: Rc::new(RefCell::new(TrieNode::new())), |
370 | | - } |
371 | | - } |
372 | | - |
373 | | - fn insert(&self, word: String) { |
374 | | - let char_vec: Vec<char> = word.chars().collect(); |
375 | | - // Get the clone of current root node |
376 | | - let mut root = Rc::clone(&self.root); |
377 | | - for c in &char_vec { |
378 | | - if !root.borrow().child.contains_key(c) { |
379 | | - // We need to manually create the entry |
380 | | - root.borrow_mut() |
381 | | - .child |
382 | | - .insert(*c, Rc::new(RefCell::new(TrieNode::new()))); |
383 | | - } |
384 | | - // Get the child node |
385 | | - let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); |
386 | | - root = root_clone; |
387 | | - } |
388 | | - { |
389 | | - root.borrow_mut().flag = true; |
390 | | - } |
391 | | - } |
392 | | - |
393 | | - fn search(&self, word: String) -> bool { |
394 | | - let char_vec: Vec<char> = word.chars().collect(); |
395 | | - // Get the clone of current root node |
396 | | - let mut root = Rc::clone(&self.root); |
397 | | - for c in &char_vec { |
398 | | - if !root.borrow().child.contains_key(c) { |
399 | | - return false; |
400 | | - } |
401 | | - // Get the child node |
402 | | - let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); |
403 | | - root = root_clone; |
404 | | - } |
405 | | - let flag = root.borrow().flag; |
406 | | - flag |
407 | | - } |
408 | | - |
409 | | - fn starts_with(&self, prefix: String) -> bool { |
410 | | - let char_vec: Vec<char> = prefix.chars().collect(); |
411 | | - // Get the clone of current root node |
412 | | - let mut root = Rc::clone(&self.root); |
413 | | - for c in &char_vec { |
414 | | - if !root.borrow().child.contains_key(c) { |
415 | | - return false; |
416 | | - } |
417 | | - // Get the child node |
418 | | - let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); |
419 | | - root = root_clone; |
420 | | - } |
421 | | - true |
422 | | - } |
423 | | -} |
424 | | -``` |
425 | | - |
426 | | -#### JavaScript |
427 | | - |
428 | | -```js |
429 | | -/** |
430 | | - * Initialize your data structure here. |
431 | | - */ |
432 | | -var Trie = function () { |
433 | | - this.children = {}; |
434 | | -}; |
435 | | - |
436 | | -/** |
437 | | - * Inserts a word into the trie. |
438 | | - * @param {string} word |
439 | | - * @return {void} |
440 | | - */ |
441 | | -Trie.prototype.insert = function (word) { |
442 | | - let node = this.children; |
443 | | - for (let char of word) { |
444 | | - if (!node[char]) { |
445 | | - node[char] = {}; |
446 | | - } |
447 | | - node = node[char]; |
448 | | - } |
449 | | - node.isEnd = true; |
450 | | -}; |
451 | | - |
452 | | -/** |
453 | | - * Returns if the word is in the trie. |
454 | | - * @param {string} word |
455 | | - * @return {boolean} |
456 | | - */ |
457 | | -Trie.prototype.search = function (word) { |
458 | | - let node = this.searchPrefix(word); |
459 | | - return node != undefined && node.isEnd != undefined; |
460 | | -}; |
461 | | - |
462 | | -Trie.prototype.searchPrefix = function (prefix) { |
463 | | - let node = this.children; |
464 | | - for (let char of prefix) { |
465 | | - if (!node[char]) return false; |
466 | | - node = node[char]; |
467 | | - } |
468 | | - return node; |
469 | | -}; |
470 | | - |
471 | | -/** |
472 | | - * Returns if there is any word in the trie that starts with the given prefix. |
473 | | - * @param {string} prefix |
474 | | - * @return {boolean} |
475 | | - */ |
476 | | -Trie.prototype.startsWith = function (prefix) { |
477 | | - return this.searchPrefix(prefix); |
478 | | -}; |
479 | | - |
480 | | -/** |
481 | | - * Your Trie object will be instantiated and called as such: |
482 | | - * var obj = new Trie() |
483 | | - * obj.insert(word) |
484 | | - * var param_2 = obj.search(word) |
485 | | - * var param_3 = obj.startsWith(prefix) |
486 | | - */ |
487 | | -``` |
488 | | - |
489 | | -#### C# |
490 | | - |
491 | | -```cs |
492 | | -public class Trie { |
493 | | - bool isEnd; |
494 | | - Trie[] children = new Trie[26]; |
495 | | - |
496 | | - public Trie() { |
497 | | - |
498 | | - } |
499 | | - |
500 | | - public void Insert(string word) { |
501 | | - Trie node = this; |
502 | | - foreach (var c in word) |
503 | | - { |
504 | | - var idx = c - 'a'; |
505 | | - node.children[idx] ??= new Trie(); |
506 | | - node = node.children[idx]; |
507 | | - } |
508 | | - node.isEnd = true; |
509 | | - } |
510 | | - |
511 | | - public bool Search(string word) { |
512 | | - Trie node = SearchPrefix(word); |
513 | | - return node != null && node.isEnd; |
514 | | - } |
515 | | - |
516 | | - public bool StartsWith(string prefix) { |
517 | | - Trie node = SearchPrefix(prefix); |
518 | | - return node != null; |
519 | | - } |
520 | | - |
521 | | - private Trie SearchPrefix(string s) { |
522 | | - Trie node = this; |
523 | | - foreach (var c in s) |
524 | | - { |
525 | | - var idx = c - 'a'; |
526 | | - if (node.children[idx] == null) |
527 | | - { |
528 | | - return null; |
529 | | - } |
530 | | - node = node.children[idx]; |
531 | | - } |
532 | | - return node; |
533 | | - } |
534 | | -} |
535 | | - |
536 | | -/** |
537 | | - * Your Trie object will be instantiated and called as such: |
538 | | - * Trie obj = new Trie(); |
539 | | - * obj.Insert(word); |
540 | | - * bool param_2 = obj.Search(word); |
541 | | - * bool param_3 = obj.StartsWith(prefix); |
542 | | - */ |
543 | | -``` |
544 | | - |
545 | 221 | <!-- tabs:end --> |
546 | 222 |
|
547 | 223 | <!-- solution:end --> |
|
0 commit comments