-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79.WordSearch.go
36 lines (27 loc) · 1.14 KB
/
79.WordSearch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can 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.
Example 1:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
*/
func exist(board [][]byte, word string) bool {
if len(board) == 0 {return false}
w, h := len(board[0]), len(board)
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
if search(board, word, 0, i, j) == true {return true}
}
}
return false
}
func search(board [][]byte, word string, d, x, y int) bool {
if x < 0 || x == len(board[0]) || y < 0 || y == len(board) || word[d] != board[y][x] {
return false
}
if d == len(word)-1 {return true}
curr := board[y][x]
board[y][x] = 0
found := search(board, word, d+1, x+1, y) || search(board, word, d+1, x-1, y) || search(board, word, d+1, x, y+1) || search(board, word, d+1, x, y-1)
board[y][x] = curr
return found
}