halfrost / LeetCode-Go

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
https://books.halfrost.com/leetcode
MIT License
32.91k stars 5.7k forks source link

79. word search go #290

Open AvaniD19 opened 2 weeks ago

AvaniD19 commented 2 weeks ago

Base Case Check: In your search Word 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.

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 search Word 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.

halfrost commented 2 weeks ago

@AvaniD19 Hi, In the following code snippet, when index reaches Len(word) - 1, the condition board[x][y] == word[index] is checked. This ensures that the entire word matches exactly, which corresponds to the Base Case Check you mentioned.

    if index == len(word)-1 {
        return board[x][y] == word[index]
    }

As for the Visited Check, while traversing through the 2D board, the DFS function updates the Visited state for each cell. Therefore, the Visited state of the last character is also updated accordingly.

AvaniD19 commented 2 weeks ago

Sure, will check, thank you for the help