Skip to content
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
Binary file added leetcode/201-300/0212.Word-Search-II/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added leetcode/201-300/0212.Word-Search-II/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 13 additions & 14 deletions leetcode/201-300/0212.Word-Search-II/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [212.Word Search II][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given an `m x n` `board` of characters and a list of strings `words`, return all words on the board.

Each word must be constructed from letters of sequentially **adjacent cells**, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

**Example 1:**

**Example 1:**
![1](./1.jpg)

```
Input: a = "11", b = "1"
Output: "100"
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.jpg)

### 思路1
> ...
Word Search II
```go
```

Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []
```

## 结语

Expand Down
66 changes: 64 additions & 2 deletions leetcode/201-300/0212.Word-Search-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
package Solution

func Solution(x bool) bool {
return x
type trieNode212 struct {
child [26]*trieNode212
word string
}

func buildTrieNode212(words []string) *trieNode212 {
root := &trieNode212{}
for _, word := range words {
walker := root
// a, b
for i, b := range word {
index := b - 'a'
if walker.child[index] == nil {
walker.child[index] = &trieNode212{}
}
walker = walker.child[index]
if i == len(word)-1 {
walker.word = word
}
}
}
return root
}
func Solution(board [][]byte, words []string) []string {
rows, cols := len(board), len(board[0])
tree := buildTrieNode212(words)
var (
search func(int, int, *trieNode212)
dirs = [][]int{
{0, 1}, {0, -1}, {1, 0}, {-1, 0},
}
)
found := map[string]struct{}{}
search = func(x, y int, tree *trieNode212) {
if x < 0 || x >= rows || y < 0 || y >= cols || board[x][y] == '|' {
return
}
index := board[x][y] - 'a'
node := tree.child[index]
if node == nil {
return
}
if node.word != "" {
found[node.word] = struct{}{}
}
source := board[x][y]
board[x][y] = '|'
for _, dir := range dirs {
nx, ny := x+dir[0], y+dir[1]
walker := node
search(nx, ny, walker)
}
board[x][y] = source
}
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
search(r, c, tree)
}
}
var ret []string
for key := range found {
ret = append(ret, key)
}
return ret
}
27 changes: 17 additions & 10 deletions leetcode/201-300/0212.Word-Search-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,37 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
board [][]byte
words []string
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]byte{
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'},
}, []string{"oath", "pea", "eat", "rain"}, []string{"oath", "eat"}},
{"TestCase2", [][]byte{
{'a', 'b'}, {'c', 'd'},
}, []string{"abcd"}, nil},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.board, c.words)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.board, c.words)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading