-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_search.go
41 lines (39 loc) · 990 Bytes
/
word_search.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
37
38
39
40
41
package backtracking
func WordSearch(board [][]byte, word string) bool {
// Special case
if len(board) == 0 || len(word) == 0 {
return false
}
// Dimensions of the board
m, n := len(board), len(board[0])
// Process cells on the board
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if board[i][j] == word[0] && Search(board, i, j, m, n, word, 0) {
return true
}
}
}
return false
}
func Search(board [][]byte, i int, j int, m int, n int, word string, index int) bool {
// Base case
if index == len(word) {
return true
}
// Handle out of bound indices
if i >= m || i < 0 || j >= n || j < 0 || board[i][j] != word[index] {
return false
}
if board[i][j] == '#' {
return false
}
temp := board[i][j]
board[i][j] = '#'
found := Search(board, i-1, j, m, n, word, index+1) ||
Search(board, i+1, j, m, n, word, index+1) ||
Search(board, i, j-1, m, n, word, index+1) ||
Search(board, i, j+1, m, n, word, index+1)
board[i][j] = temp
return found
}