Skip to content

Commit c5b210f

Browse files
committed
208 solved 💫
1 parent b64da5f commit c5b210f

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ continually updating 😃.
111111
* [111. Minimum Depth of Binary Tree](src/0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)   *`binary tree;`*   *`dfs`*
112112
* [112. Path Sum](src/0112_path_sum/path_sum.go)   *`binary tree;`*   *`dfs`*
113113
* [144. Binary Tree Preorder Traversal](src/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)   *`binary tree;`*   *`pre-order traversal`*
114-
* [226. Invert Binary Tree](src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree;`*
115-
* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree;`*
114+
* [208. Implement Trie (Prefix Tree)](src/0208_implement_trie_prefix_tree/impltrie.go)   *`trie`*
115+
* [226. Invert Binary Tree](src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree`*
116+
* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree`*
116117
* [257. Binary Tree Paths](src/0257_binary_tree_paths/binary_tree_paths.go)   *`binary tree`*
117118
* [307. Range Sum Query - Mutable](src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)   *`segment tree`*
118119
* [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
208. Implement Trie (Prefix Tree)
3+
https://leetcode.com/problems/implement-trie-prefix-tree/
4+
5+
Implement a trie with insert, search, and startsWith methods.
6+
7+
Note:
8+
You may assume that all inputs are consist of lowercase letters a-z.
9+
All inputs are guaranteed to be non-empty strings.
10+
*/
11+
// time: 2019-01-31
12+
13+
package impltree
14+
15+
type node struct {
16+
isWord bool
17+
next map[byte]*node
18+
}
19+
20+
// Trie prefix tree
21+
type Trie struct {
22+
root *node
23+
size int
24+
}
25+
26+
// Constructor initialize data structure here.
27+
func Constructor() Trie {
28+
return Trie{root: &node{next: make(map[byte]*node)}}
29+
}
30+
31+
// Insert inserts a word into the trie.
32+
func (trie *Trie) Insert(word string) {
33+
cur := trie.root
34+
35+
for i := 0; i < len(word); i++ {
36+
if _, ok := cur.next[word[i]]; !ok {
37+
cur.next[word[i]] = &node{next: make(map[byte]*node)}
38+
}
39+
cur, _ = cur.next[word[i]]
40+
}
41+
if !cur.isWord {
42+
cur.isWord = true
43+
trie.size++
44+
}
45+
}
46+
47+
// Search returns if the word is the trie.
48+
func (trie *Trie) Search(word string) bool {
49+
cur := trie.root
50+
51+
for i := 0; i < len(word); i++ {
52+
if _, ok := cur.next[word[i]]; !ok {
53+
return false
54+
}
55+
cur, _ = cur.next[word[i]]
56+
}
57+
58+
return cur.isWord
59+
}
60+
61+
// StartsWith returns if there is any word in the trie that starts with the given prefix.
62+
func (trie *Trie) StartsWith(prefix string) bool {
63+
cur := trie.root
64+
65+
for i := 0; i < len(prefix); i++ {
66+
if _, ok := cur.next[prefix[i]]; !ok {
67+
return false
68+
}
69+
cur, _ = cur.next[prefix[i]]
70+
}
71+
return true
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package impltree
2+
3+
import "testing"
4+
5+
func TestImplTrie(t *testing.T) {
6+
trie := Constructor()
7+
trie.Insert("apple")
8+
9+
for i, j := range map[string]bool{"apple": true, "app": false} {
10+
if res := trie.Search(i); res != j {
11+
t.Errorf("expected %t, got %t", j, res)
12+
}
13+
}
14+
15+
for i, j := range map[string]bool{"app": true, "as": false} {
16+
if res := trie.StartsWith(i); res != j {
17+
t.Errorf("expected %t, got %t", j, res)
18+
}
19+
}
20+
21+
trie.Insert("app")
22+
if res := trie.Search("app"); res != true {
23+
t.Errorf("expected %t, got %t", true, res)
24+
}
25+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
|0200|[200. Number of Islands](0200_number_of_island/number_of_island.go)|Medium|*`dfs;`* *`bfs`*|
6969
|0203|[203. Remove Linked List Elements](0203_remove_linked_list_elements/remove_linked_list_elements.go)|Easy|*`linked list`*|
7070
|0206|[206. Reverse Linked List](0206_reverse_linked_list/reverse_linked_list.go)|Easy|*`linked list`*|
71+
|0208|[208. Implement Trie (Prefix Tree)](0208_implement_trie_prefix_tree/impltrie.go)|Medium|*`trie`*|
7172
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
7273
|0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*|
7374
|0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|

0 commit comments

Comments
 (0)