Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 79. Word Search.go #289

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions leetcode/0079.Word-Search/79. Word Search.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,72 @@ func searchWord(board [][]byte, visited [][]bool, word string, index, x, y int)
}
visited[x][y] = false
}
return false
}
changes:
Base Case Check: In your searchWord function, you should check if index is equal to len(word) - 1, but also need to ensure that the character at index matches the character at the current position before returning true. You should adjust this condition to account for reaching the end of the word correctly.

Visited Check: You should also ensure that the last character in the word is checked correctly.

Here’s the corrected version of your code:

go
Copy code
package leetcode

var dir = [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}

func exist(board [][]byte, word string) bool {
visited := make([][]bool, len(board))
for i := range visited {
visited[i] = make([]bool, len(board[0]))
}
for i := range board {
for j := range board[i] {
if searchWord(board, visited, word, 0, i, j) {
return true
}
}
}
return false
}

func isInBoard(board [][]byte, x, y int) bool {
return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
}

func searchWord(board [][]byte, visited [][]bool, word string, index, x, y int) bool {
// Check if we have matched the entire word
if index == len(word) {
return true
}
// Check boundaries and if the cell is already visited or doesn't match the word
if !isInBoard(board, x, y) || visited[x][y] || board[x][y] != word[index] {
return false
}

// Mark the cell as visited
visited[x][y] = true

// Explore all 4 directions
for i := 0; i < 4; i++ {
nx := x + dir[i][0]
ny := y + dir[i][1]
if searchWord(board, visited, word, index+1, nx, ny) {
return true
}
}

// Backtrack: unmark the cell as visited
visited[x][y] = false
return false
}
Key Changes:
Base Case Update: The base case now checks if index is equal to len(word), indicating a full match of the word.
Boundary and Match Check: The conditions in searchWord ensure the current cell is valid, not visited, and matches the corresponding character in the word before proceeding.
Backtracking Logic: The backtracking logic is preserved but placed correctly to ensure valid states.