forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0079-word-search.go
38 lines (30 loc) · 688 Bytes
/
0079-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
package main
funcexist(board [][]byte, wordstring) bool {
n:=len(board)
m:=len(board[0])
vardfsfunc(iint, jint, currint) bool
dfs=func(iint, jint, currint) bool {
ifi<0||j<0||i>=n||j>=m||curr==len(word) {
returnfalse
}
ifboard[i][j] !=word[curr] ||board[i][j] =='*' {
returnfalse
}
ifcurr==len(word)-1 {
returntrue
}
tmp:=board[i][j]
board[i][j] ='*'
res:=dfs(i+1, j, curr+1) ||dfs(i-1, j, curr+1) ||dfs(i, j-1, curr+1) ||dfs(i, j+1, curr+1)
board[i][j] =tmp
returnres
}
fori:=0; i<n; i++ {
forj:=0; j<m; j++ {
ifdfs(i, j, 0) {
returntrue
}
}
}
returnfalse
}