Skip to content

Commit

Permalink
Time: 656 ms (12.33%), Space: 4.3 MB (17.85%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Dec 5, 2024
1 parent 07cf85d commit f7bbbf5
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions 0079-word-search/0079-word-search.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
func exist(board [][]byte, word string) bool {
lenRow, lenCol := len(board), len(board[0])
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}] {
if i < 0 || j < 0 || i == lenRow || j == lenCol || mMap[[2]int{i, j}]|| board[i][j] != word[l] {
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
}

Expand Down

0 comments on commit f7bbbf5

Please sign in to comment.