|
| 1 | +package leetcode |
| 2 | + |
| 3 | +/* |
| 4 | + * @lc app=leetcode.cn id=208 lang=golang |
| 5 | + * |
| 6 | + * [208] 实现 Trie (前缀树) |
| 7 | + * |
| 8 | + * https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/ |
| 9 | + * |
| 10 | + * algorithms |
| 11 | + * Medium (61.14%) |
| 12 | + * Likes: 129 |
| 13 | + * Dislikes: 0 |
| 14 | + * Total Accepted: 13.4K |
| 15 | + * Total Submissions: 21.8K |
| 16 | + * Testcase Example: '["Trie","insert","search","search","startsWith","insert","search"]\n[[],["apple"],["apple"],["app"],["app"],["app"],["app"]]' |
| 17 | + * |
| 18 | + * 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。 |
| 19 | + * |
| 20 | + * 示例: |
| 21 | + * |
| 22 | + * Trie trie = new Trie(); |
| 23 | + * |
| 24 | + * trie.insert("apple"); |
| 25 | + * trie.search("apple"); // 返回 true |
| 26 | + * trie.search("app"); // 返回 false |
| 27 | + * trie.startsWith("app"); // 返回 true |
| 28 | + * trie.insert("app"); |
| 29 | + * trie.search("app"); // 返回 true |
| 30 | + * |
| 31 | + * 说明: |
| 32 | + * |
| 33 | + * |
| 34 | + * 你可以假设所有的输入都是由小写字母 a-z 构成的。 |
| 35 | + * 保证所有输入均为非空字符串。 |
| 36 | + * |
| 37 | + * |
| 38 | + */ |
| 39 | + |
| 40 | +// @lc code=start |
| 41 | +type Node struct { |
| 42 | + Child map[rune]*Node |
| 43 | + Value bool |
| 44 | +} |
| 45 | + |
| 46 | +type Trie struct { |
| 47 | + Root *Node |
| 48 | +} |
| 49 | + |
| 50 | +/** Initialize your data structure here. */ |
| 51 | +func Constructor() Trie { |
| 52 | + return Trie{ |
| 53 | + Root: &Node{ |
| 54 | + Child: map[rune]*Node{}, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** Inserts a word into the trie. */ |
| 60 | +func (this *Trie) Insert(word string) { |
| 61 | + cur := this.Root |
| 62 | + for _, c := range word { |
| 63 | + if _, ok := cur.Child[c]; !ok { |
| 64 | + cur.Child[c] = &Node{ |
| 65 | + Child: map[rune]*Node{}, |
| 66 | + } |
| 67 | + } |
| 68 | + cur = cur.Child[c] |
| 69 | + } |
| 70 | + cur.Value = true |
| 71 | +} |
| 72 | + |
| 73 | +/** Returns if the word is in the trie. */ |
| 74 | +func (this *Trie) Search(word string) bool { |
| 75 | + cur := this.Root |
| 76 | + for _, c := range word { |
| 77 | + if _, ok := cur.Child[c]; !ok { |
| 78 | + return false |
| 79 | + } |
| 80 | + cur = cur.Child[c] |
| 81 | + } |
| 82 | + return cur.Value |
| 83 | +} |
| 84 | + |
| 85 | +/** Returns if there is any word in the trie that starts with the given prefix. */ |
| 86 | +func (this *Trie) StartsWith(prefix string) bool { |
| 87 | + cur := this.Root |
| 88 | + for _, c := range prefix { |
| 89 | + if _, ok := cur.Child[c]; !ok { |
| 90 | + return false |
| 91 | + } |
| 92 | + cur = cur.Child[c] |
| 93 | + } |
| 94 | + return true |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Your Trie object will be instantiated and called as such: |
| 99 | + * obj := Constructor(); |
| 100 | + * obj.Insert(word); |
| 101 | + * param_2 := obj.Search(word); |
| 102 | + * param_3 := obj.StartsWith(prefix); |
| 103 | + */ |
| 104 | +// @lc code=end |
0 commit comments