|
| 1 | +[](https://github.com/javadev/LeetCode-in-All) |
| 2 | +[](https://github.com/javadev/LeetCode-in-All/fork) |
| 3 | + |
| 4 | +## 208\. Implement Trie (Prefix Tree) |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. |
| 9 | + |
| 10 | +Implement the Trie class: |
| 11 | + |
| 12 | +* `Trie()` Initializes the trie object. |
| 13 | +* `void insert(String word)` Inserts the string `word` into the trie. |
| 14 | +* `boolean search(String word)` Returns `true` if the string `word` is in the trie (i.e., was inserted before), and `false` otherwise. |
| 15 | +* `boolean startsWith(String prefix)` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`, and `false` otherwise. |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +**Input** ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]] |
| 20 | + |
| 21 | +**Output:** [null, null, true, false, true, null, true] |
| 22 | + |
| 23 | +**Explanation:** |
| 24 | + |
| 25 | +Trie trie = new Trie(); |
| 26 | + |
| 27 | +trie.insert("apple"); |
| 28 | + |
| 29 | +trie.search("apple"); // return True |
| 30 | + |
| 31 | +trie.search("app"); // return False |
| 32 | + |
| 33 | +trie.startsWith("app"); // return True |
| 34 | + |
| 35 | +trie.insert("app"); |
| 36 | + |
| 37 | +trie.search("app"); // return True |
| 38 | + |
| 39 | +**Constraints:** |
| 40 | + |
| 41 | +* `1 <= word.length, prefix.length <= 2000` |
| 42 | +* `word` and `prefix` consist only of lowercase English letters. |
| 43 | +* At most <code>3 * 10<sup>4</sup></code> calls **in total** will be made to `insert`, `search`, and `startsWith`. |
| 44 | + |
| 45 | +## Solution |
| 46 | + |
| 47 | +```racket |
| 48 | +(define trie% |
| 49 | + (class object% |
| 50 | + (super-new) |
| 51 | +
|
| 52 | + ;; Define TrieNode struct |
| 53 | + (struct trie-node (children is-word?) #:mutable) |
| 54 | +
|
| 55 | + ;; Root node of the Trie |
| 56 | + (init-field) |
| 57 | + (define root (trie-node (make-hash) #f)) |
| 58 | + (define start-with? #f) |
| 59 | +
|
| 60 | + ;; Inserts a word into the trie. |
| 61 | + (define/public (insert word) |
| 62 | + (define (insert-helper node word idx) |
| 63 | + (if (= idx (string-length word)) |
| 64 | + (set-trie-node-is-word?! node #t) |
| 65 | + (let* ([ch (string-ref word idx)] |
| 66 | + [children (trie-node-children node)] |
| 67 | + [next-node (hash-ref children ch (lambda () (trie-node (make-hash) #f)))]) |
| 68 | + (hash-set! children ch next-node) |
| 69 | + (insert-helper next-node word (+ idx 1))))) |
| 70 | + (insert-helper root word 0)) |
| 71 | +
|
| 72 | + ;; Searches for a word in the trie. |
| 73 | + (define/public (search word) |
| 74 | + (define (search-helper node word idx) |
| 75 | + (if (= idx (string-length word)) |
| 76 | + (begin |
| 77 | + (set! start-with? #t) |
| 78 | + (trie-node-is-word? node)) |
| 79 | + (let* ([ch (string-ref word idx)] |
| 80 | + [children (trie-node-children node)] |
| 81 | + [next-node (hash-ref children ch #f)]) |
| 82 | + (if next-node |
| 83 | + (search-helper next-node word (+ idx 1)) |
| 84 | + (begin |
| 85 | + (set! start-with? #f) |
| 86 | + #f))))) |
| 87 | + (search-helper root word 0)) |
| 88 | +
|
| 89 | + ;; Checks if any word in the trie starts with the given prefix. |
| 90 | + (define/public (starts-with prefix) |
| 91 | + (send this search prefix) |
| 92 | + start-with?))) |
| 93 | +
|
| 94 | +;; Your trie% object will be instantiated and called as such: |
| 95 | +;; (define obj (new trie%)) |
| 96 | +;; (send obj insert word) |
| 97 | +;; (define param_2 (send obj search word)) |
| 98 | +;; (define param_3 (send obj starts-with prefix)) |
| 99 | +``` |
0 commit comments