Skip to content

208. Implement Trie (Prefix Tree) 💫 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`*
Expand Down
72 changes: 72 additions & 0 deletions src/0208_implement_trie_prefix_tree/impltrie.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions src/0208_implement_trie_prefix_tree/impltrie_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`*|
Expand Down