diff --git a/leetcode/201-300/0212.Word-Search-II/1.jpg b/leetcode/201-300/0212.Word-Search-II/1.jpg new file mode 100644 index 000000000..9f569294f Binary files /dev/null and b/leetcode/201-300/0212.Word-Search-II/1.jpg differ diff --git a/leetcode/201-300/0212.Word-Search-II/2.jpg b/leetcode/201-300/0212.Word-Search-II/2.jpg new file mode 100644 index 000000000..7faa406ff Binary files /dev/null and b/leetcode/201-300/0212.Word-Search-II/2.jpg differ diff --git a/leetcode/201-300/0212.Word-Search-II/README.md b/leetcode/201-300/0212.Word-Search-II/README.md index 66a401852..c156c11fe 100644 --- a/leetcode/201-300/0212.Word-Search-II/README.md +++ b/leetcode/201-300/0212.Word-Search-II/README.md @@ -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: [] +``` ## 结语 diff --git a/leetcode/201-300/0212.Word-Search-II/Solution.go b/leetcode/201-300/0212.Word-Search-II/Solution.go index d115ccf5e..dee4bb22a 100644 --- a/leetcode/201-300/0212.Word-Search-II/Solution.go +++ b/leetcode/201-300/0212.Word-Search-II/Solution.go @@ -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 } diff --git a/leetcode/201-300/0212.Word-Search-II/Solution_test.go b/leetcode/201-300/0212.Word-Search-II/Solution_test.go index 14ff50eb4..d9e83acd8 100644 --- a/leetcode/201-300/0212.Word-Search-II/Solution_test.go +++ b/leetcode/201-300/0212.Word-Search-II/Solution_test.go @@ -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() { }