diff --git a/README.md b/README.md index c029098..ddb2397 100644 --- a/README.md +++ b/README.md @@ -111,8 +111,9 @@ continually updating 😃. * [111. Minimum Depth of Binary Tree](src/0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)   *`binary tree;`*   *`dfs`* * [112. Path Sum](src/0112_path_sum/path_sum.go)   *`binary tree;`*   *`dfs`* * [144. Binary Tree Preorder Traversal](src/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)   *`binary tree;`*   *`pre-order traversal`* -* [226. Invert Binary Tree](src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree;`* -* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree;`* +* [208. Implement Trie (Prefix Tree)](src/0208_implement_trie_prefix_tree/impltrie.go)   *`trie`* +* [226. Invert Binary Tree](src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree`* +* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree`* * [257. Binary Tree Paths](src/0257_binary_tree_paths/binary_tree_paths.go)   *`binary tree`* * [307. Range Sum Query - Mutable](src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)   *`segment tree`* * [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`* diff --git a/src/0208_implement_trie_prefix_tree/impltrie.go b/src/0208_implement_trie_prefix_tree/impltrie.go new file mode 100644 index 0000000..38ac621 --- /dev/null +++ b/src/0208_implement_trie_prefix_tree/impltrie.go @@ -0,0 +1,72 @@ +/* +208. Implement Trie (Prefix Tree) +https://leetcode.com/problems/implement-trie-prefix-tree/ + +Implement a trie with insert, search, and startsWith methods. + +Note: +You may assume that all inputs are consist of lowercase letters a-z. +All inputs are guaranteed to be non-empty strings. +*/ +// time: 2019-01-31 + +package impltree + +type node struct { + isWord bool + next map[byte]*node +} + +// Trie prefix tree +type Trie struct { + root *node + size int +} + +// Constructor initialize data structure here. +func Constructor() Trie { + return Trie{root: &node{next: make(map[byte]*node)}} +} + +// Insert inserts a word into the trie. +func (trie *Trie) Insert(word string) { + cur := trie.root + + for i := 0; i < len(word); i++ { + if _, ok := cur.next[word[i]]; !ok { + cur.next[word[i]] = &node{next: make(map[byte]*node)} + } + cur, _ = cur.next[word[i]] + } + if !cur.isWord { + cur.isWord = true + trie.size++ + } +} + +// Search returns if the word is the trie. +func (trie *Trie) Search(word string) bool { + cur := trie.root + + for i := 0; i < len(word); i++ { + if _, ok := cur.next[word[i]]; !ok { + return false + } + cur, _ = cur.next[word[i]] + } + + return cur.isWord +} + +// StartsWith returns if there is any word in the trie that starts with the given prefix. +func (trie *Trie) StartsWith(prefix string) bool { + cur := trie.root + + for i := 0; i < len(prefix); i++ { + if _, ok := cur.next[prefix[i]]; !ok { + return false + } + cur, _ = cur.next[prefix[i]] + } + return true +} diff --git a/src/0208_implement_trie_prefix_tree/impltrie_test.go b/src/0208_implement_trie_prefix_tree/impltrie_test.go new file mode 100644 index 0000000..a2b695c --- /dev/null +++ b/src/0208_implement_trie_prefix_tree/impltrie_test.go @@ -0,0 +1,25 @@ +package impltree + +import "testing" + +func TestImplTrie(t *testing.T) { + trie := Constructor() + trie.Insert("apple") + + for i, j := range map[string]bool{"apple": true, "app": false, "hello": false} { + if res := trie.Search(i); res != j { + t.Errorf("expected %t, got %t", j, res) + } + } + + for i, j := range map[string]bool{"app": true, "as": false} { + if res := trie.StartsWith(i); res != j { + t.Errorf("expected %t, got %t", j, res) + } + } + + trie.Insert("app") + if res := trie.Search("app"); res != true { + t.Errorf("expected %t, got %t", true, res) + } +} diff --git a/src/README.md b/src/README.md index c41f8af..987cb68 100644 --- a/src/README.md +++ b/src/README.md @@ -68,6 +68,7 @@ |0200|[200. Number of Islands](0200_number_of_island/number_of_island.go)|Medium|*`dfs;`* *`bfs`*| |0203|[203. Remove Linked List Elements](0203_remove_linked_list_elements/remove_linked_list_elements.go)|Easy|*`linked list`*| |0206|[206. Reverse Linked List](0206_reverse_linked_list/reverse_linked_list.go)|Easy|*`linked list`*| +|0208|[208. Implement Trie (Prefix Tree)](0208_implement_trie_prefix_tree/impltrie.go)|Medium|*`trie`*| |0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*| |0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*| |0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|