mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
render chapter two
This commit is contained in:
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
type WordDictionary struct {
|
||||
children map[rune]*WordDictionary
|
||||
isWord bool
|
||||
}
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
func Constructor211() WordDictionary {
|
||||
return WordDictionary{children: make(map[rune]*WordDictionary)}
|
||||
}
|
||||
|
||||
/** Adds a word into the data structure. */
|
||||
func (this *WordDictionary) AddWord(word string) {
|
||||
parent := this
|
||||
for _, ch := range word {
|
||||
if child, ok := parent.children[ch]; ok {
|
||||
parent = child
|
||||
} else {
|
||||
newChild := &WordDictionary{children: make(map[rune]*WordDictionary)}
|
||||
parent.children[ch] = newChild
|
||||
parent = newChild
|
||||
}
|
||||
}
|
||||
parent.isWord = true
|
||||
}
|
||||
|
||||
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
|
||||
func (this *WordDictionary) Search(word string) bool {
|
||||
parent := this
|
||||
for i, ch := range word {
|
||||
if rune(ch) == '.' {
|
||||
isMatched := false
|
||||
for _, v := range parent.children {
|
||||
if v.Search(word[i+1:]) {
|
||||
isMatched = true
|
||||
}
|
||||
}
|
||||
return isMatched
|
||||
} else if _, ok := parent.children[rune(ch)]; !ok {
|
||||
return false
|
||||
}
|
||||
parent = parent.children[rune(ch)]
|
||||
}
|
||||
return len(parent.children) == 0 || parent.isWord
|
||||
}
|
||||
|
||||
/**
|
||||
* Your WordDictionary object will be instantiated and called as such:
|
||||
* obj := Constructor();
|
||||
* obj.AddWord(word);
|
||||
* param_2 := obj.Search(word);
|
||||
*/
|
@ -0,0 +1,26 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Problem211(t *testing.T) {
|
||||
obj := Constructor211()
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
obj.AddWord("bad")
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
obj.AddWord("dad")
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
obj.AddWord("mad")
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
|
||||
param1 := obj.Search("pad")
|
||||
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
|
||||
param2 := obj.Search("bad")
|
||||
fmt.Printf("param_2 = %v obj = %v\n", param2, obj)
|
||||
param3 := obj.Search(".ad")
|
||||
fmt.Printf("param_3 = %v obj = %v\n", param3, obj)
|
||||
param4 := obj.Search("b..")
|
||||
fmt.Printf("param_4 = %v obj = %v\n", param4, obj)
|
||||
}
|
34
leetcode/0211.Design-Add-and-Search-Words-Data-Structure/README.md
Executable file
34
leetcode/0211.Design-Add-and-Search-Words-Data-Structure/README.md
Executable file
@ -0,0 +1,34 @@
|
||||
# [211. Design Add and Search Words Data Structure](https://leetcode.com/problems/design-add-and-search-words-data-structure/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Design a data structure that supports the following two operations:
|
||||
|
||||
void addWord(word)
|
||||
bool search(word)
|
||||
|
||||
search(word) can search a literal word or a regular expression string containing only letters `a-z` or `.`. A `.` means it can represent any one letter.
|
||||
|
||||
**Example:**
|
||||
|
||||
addWord("bad")
|
||||
addWord("dad")
|
||||
addWord("mad")
|
||||
search("pad") -> false
|
||||
search("bad") -> true
|
||||
search(".ad") -> true
|
||||
search("b..") -> true
|
||||
|
||||
**Note:**You may assume that all words are consist of lowercase letters `a-z`.
|
||||
|
||||
## 题目大意
|
||||
|
||||
设计一个支持以下两种操作的数据结构:`void addWord(word)`、`bool search(word)`。`search(word)` 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 "." 可以表示任何一个字母。
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 设计一个 `WordDictionary` 的数据结构,要求具有 `addWord(word)` 和 `search(word)` 的操作,并且具有模糊查找的功能。
|
||||
- 这一题是第 208 题的加强版,在第 208 题经典的 Trie 上加上了模糊查找的功能。其他实现一模一样。
|
Reference in New Issue
Block a user