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

79. Word Search #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from word-search
Jan 4, 2019
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
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ continually updating 😃.

### Backtracking
* [77. Combinations](src/0077_combinations/combinations.go)   *`combine`*
* [79. Word Search](src/0079_word_search/word_search.go)   *`array`*

### Binary Tree
* [94. Binary Tree Inorder Traversal](./src/0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)   *`stack`*
Expand Down
61 changes: 61 additions & 0 deletions src/0079_word_search/word_search.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
79. Word Search
https://leetcode.com/problems/word-search/

Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell,
where "adjacent" cells are those horizontally or vertically neighboring.
The same letter cell may not be used more than once.
*/
// 2019年01月04日

package wordsearch

var (
d = [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
m, n int
visited [][]bool
)

// backtracking
// time complexity: O(m*n*m*n)
// space complexity: O(m*n)
func exist(board [][]byte, word string) bool {
m = len(board)
n = len(board[0])
visited = make([][]bool, m)
for i := 0; i < m; i++ {
visited[i] = make([]bool, n)
}
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[i]); j++ {
if searchWord(board, word, 0, i, j) {
return true
}
}
}
return false
}

func searchWord(board [][]byte, word string, index, startX, startY int) bool {
if len(word)-1 == index {
return board[startX][startY] == word[index]
}
if board[startX][startY] == word[index] {
visited[startX][startY] = true
// 从startX, startY开始,向四个方向寻找
for i := 0; i < 4; i++ {
newX := startX + d[i][0]
newY := startY + d[i][1]
if inArea(newX, newY) && !visited[newX][newY] && searchWord(board, word, index+1, newX, newY) {
return true
}
}
visited[startX][startY] = false
}
return false
}

func inArea(x, y int) bool {
return x >= 0 && x < m && y >= 0 && y < n
}
22 changes: 22 additions & 0 deletions src/0079_word_search/word_search_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package wordsearch

import "testing"

func TestExist(t *testing.T) {
board := [][]byte{
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'},
}
testCases := []string{
"ABCCED",
"SEE",
"ABCB",
}
expected := []bool{true, true, false}
for index, word := range testCases {
if res := exist(board, word); res != expected[index] {
t.Errorf("expected %t, got %t", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
|0075|[75. Sort Colors](0075_sort_colors/sort_colors.go)|Medium|*`sort`*|
|0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*|
|0077|[77. Combinations](0077_combinations/combinations.go)|Medium|*`backtracking;`**`combine`*|
|0079|[79. Word Search](0079_word_search/word_search.go)|Medium|*`backtracking;`**`array`*|
|0080|[80. Remove Duplicates from Sorted Array II](0080_remove_duplicates_from_sorted_array2/rdfsa2.go)|Medium|*`double index`*|
|0088|[88. Merge Sorted Array](0088_merge_sorted_array/msa.go)|Easy|*`sort`*|
|0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*|
Expand Down

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