Skip to content

Commit e8d0fed

Browse files
committed
Add solution and test-cases for problem 212
1 parent f03226b commit e8d0fed

File tree

5 files changed

+94
-26
lines changed

5 files changed

+94
-26
lines changed
13.5 KB
Loading
3.7 KB
Loading

leetcode/201-300/0212.Word-Search-II/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [212.Word Search II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an `m x n` `board` of characters and a list of strings `words`, return all words on the board.
5+
6+
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.
7+
8+
**Example 1:**
79

8-
**Example 1:**
10+
![1](./1.jpg)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
14+
Output: ["eat","oath"]
1315
```
1416

15-
## 题意
16-
> ...
17+
**Example 2:**
1718

18-
## 题解
19+
![2](./2.jpg)
1920

20-
### 思路1
21-
> ...
22-
Word Search II
23-
```go
2421
```
25-
22+
Input: board = [["a","b"],["c","d"]], words = ["abcb"]
23+
Output: []
24+
```
2625

2726
## 结语
2827

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,67 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type trieNode212 struct {
4+
child [26]*trieNode212
5+
word string
6+
}
7+
8+
func buildTrieNode212(words []string) *trieNode212 {
9+
root := &trieNode212{}
10+
for _, word := range words {
11+
walker := root
12+
// a, b
13+
for i, b := range word {
14+
index := b - 'a'
15+
if walker.child[index] == nil {
16+
walker.child[index] = &trieNode212{}
17+
}
18+
walker = walker.child[index]
19+
if i == len(word)-1 {
20+
walker.word = word
21+
}
22+
}
23+
}
24+
return root
25+
}
26+
func Solution(board [][]byte, words []string) []string {
27+
rows, cols := len(board), len(board[0])
28+
tree := buildTrieNode212(words)
29+
var (
30+
search func(int, int, *trieNode212)
31+
dirs = [][]int{
32+
{0, 1}, {0, -1}, {1, 0}, {-1, 0},
33+
}
34+
)
35+
found := map[string]struct{}{}
36+
search = func(x, y int, tree *trieNode212) {
37+
if x < 0 || x >= rows || y < 0 || y >= cols || board[x][y] == '|' {
38+
return
39+
}
40+
index := board[x][y] - 'a'
41+
node := tree.child[index]
42+
if node == nil {
43+
return
44+
}
45+
if node.word != "" {
46+
found[node.word] = struct{}{}
47+
}
48+
source := board[x][y]
49+
board[x][y] = '|'
50+
for _, dir := range dirs {
51+
nx, ny := x+dir[0], y+dir[1]
52+
walker := node
53+
search(nx, ny, walker)
54+
}
55+
board[x][y] = source
56+
}
57+
for r := 0; r < rows; r++ {
58+
for c := 0; c < cols; c++ {
59+
search(r, c, tree)
60+
}
61+
}
62+
var ret []string
63+
for key := range found {
64+
ret = append(ret, key)
65+
}
66+
return ret
567
}

leetcode/201-300/0212.Word-Search-II/Solution_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,37 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
board [][]byte
14+
words []string
15+
expect []string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]byte{
18+
{'o', 'a', 'a', 'n'},
19+
{'e', 't', 'a', 'e'},
20+
{'i', 'h', 'k', 'r'},
21+
{'i', 'f', 'l', 'v'},
22+
}, []string{"oath", "pea", "eat", "rain"}, []string{"oath", "eat"}},
23+
{"TestCase2", [][]byte{
24+
{'a', 'b'}, {'c', 'd'},
25+
}, []string{"abcd"}, nil},
1926
}
2027

2128
// 开始测试
2229
for i, c := range cases {
2330
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
31+
got := Solution(c.board, c.words)
2532
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
34+
c.expect, got, c.board, c.words)
2835
}
2936
})
3037
}
3138
}
3239

33-
// 压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
// 使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}

0 commit comments

Comments
 (0)