-
Notifications
You must be signed in to change notification settings - Fork 46
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
Merged
79. Word Search #40
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
src/0079_word_search/word_search.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.