Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1c451d

Browse files
authored
211. Add and Search Word - Data structure design💥. (#73)
* 211 solved💥. * add more ut case for 211🙏 * optimize code🤞🏻 * clean code
1 parent 69a8cb0 commit a1c451d

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ continually updating 😃.
114114
* [144. Binary Tree Preorder Traversal](src/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)   *`binary tree;`*   *`pre-order traversal`*
115115
* [208. Implement Trie (Prefix Tree)](src/0208_implement_trie_prefix_tree/impltrie.go)   *`trie`*
116116
* [226. Invert Binary Tree](src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree`*
117+
* [211. Add and Search Word - Data structure design](src/0211_add_and_search_word/add_and_search_word.go)   *`trie`*
117118
* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree`*
118119
* [257. Binary Tree Paths](src/0257_binary_tree_paths/binary_tree_paths.go)   *`binary tree`*
119120
* [307. Range Sum Query - Mutable](src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)   *`segment tree`*
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
211. Add and Search Word - Data structure design
3+
https://leetcode.com/problems/add-and-search-word-data-structure-design/
4+
*/
5+
// time: 2019年01月31日
6+
7+
package aasw
8+
9+
type node struct {
10+
isWord bool
11+
next map[byte]*node
12+
}
13+
14+
// WordDictionary supports two operations(addWord, search)
15+
type WordDictionary struct {
16+
root *node
17+
}
18+
19+
// Constructor initialize data structure here.
20+
func Constructor() WordDictionary {
21+
return WordDictionary{&node{next: make(map[byte]*node)}}
22+
}
23+
24+
// AddWord adds a word into the trie.
25+
func (trie *WordDictionary) AddWord(word string) {
26+
cur := trie.root
27+
for i := 0; i < len(word); i++ {
28+
if _, ok := cur.next[word[i]]; !ok {
29+
cur.next[word[i]] = &node{next: make(map[byte]*node)}
30+
}
31+
cur = cur.next[word[i]]
32+
}
33+
if !cur.isWord {
34+
cur.isWord = true
35+
}
36+
}
37+
38+
// Search returns if the word is in the trie.
39+
// a word could contain the dot character '.' to represent any ont letter.
40+
func (trie *WordDictionary) Search(word string) bool {
41+
return match(trie.root, word, 0)
42+
}
43+
44+
func match(n *node, word string, index int) bool {
45+
if index == len(word) {
46+
return n.isWord
47+
}
48+
if word[index] != '.' {
49+
if _, ok := n.next[word[index]]; !ok {
50+
return false
51+
}
52+
return match(n.next[word[index]], word, index+1)
53+
} else {
54+
for _, nextNode := range n.next {
55+
if match(nextNode, word, index+1) {
56+
return true
57+
}
58+
}
59+
return false
60+
}
61+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package aasw
2+
3+
import "testing"
4+
5+
func TestAddAndSearchWord(t *testing.T) {
6+
obj := Constructor()
7+
8+
for _, word := range []string{"bad", "dad", "mad"}{
9+
obj.AddWord(word)
10+
}
11+
12+
for i, j := range map[string]bool{"pad": false, "bad": true, ".ad": true, "b..": true, "b..d": false} {
13+
if res := obj.Search(i); res != j {
14+
t.Errorf("expected %t, got %t", j, res)
15+
}
16+
}
17+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
|0206|[206. Reverse Linked List](0206_reverse_linked_list/reverse_linked_list.go)|Easy|*`linked list`*|
7272
|0208|[208. Implement Trie (Prefix Tree)](0208_implement_trie_prefix_tree/impltrie.go)|Medium|*`trie`*|
7373
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|
74+
|0211|[211. Add and Search Word - Data structure design](0211_add_and_search_word/add_and_search_word.go)|Medium|*`trie`*|
7475
|0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*|
7576
|0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|
7677
|0219|[219. Contains Duplicate II](0219_contains_duplicate_2/contains_duplicate_2.go)|Easy|*`map`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /