Skip to content

Commit

Permalink
Time: 482 ms (19.45%), Space: 4.2 MB (17.92%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Dec 4, 2024
1 parent c60689d commit 07cf85d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 0079-word-search/0079-word-search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
func exist(board [][]byte, word string) bool {
mMap := map[[2]int]bool{}
row, col := len(board), len(board[0])

var dfs func(i, j, l int) bool
dfs = func(i, j, l int) bool {
if l == len(word) {
return true
}

if i < 0 || j < 0 || i >= row || j >= col || board[i][j] != word[l] || mMap[[2]int{i, j}] {
return false
}

mMap[[2]int{i, j}] = true
res := dfs(i + 1, j, l + 1) || dfs(i - 1, j, l + 1) || dfs(i, j + 1, l + 1) || dfs(i, j - 1, l + 1)

mMap[[2]int{i, j}] = false
return res
}

for i := range board {
for j := range board[0] {
if dfs(i, j, 0) {
return true
}
}
}

return false
}

0 comments on commit 07cf85d

Please sign in to comment.