Leetcode Problem 79. Word Search

79. Word Search

Leetcode Solutions

Backtracking with Depth-First Search

  1. Iterate over each cell in the grid.
  2. For each cell, call the recursive backtracking function with the current cell's coordinates and the word to be matched.
  3. If the first letter of the word does not match the current cell, or if the cell is out of bounds, return false.
  4. If the word is empty, return true as all characters have been matched.
  5. Mark the current cell as visited by altering its value.
  6. Recursively explore the next cell in all four directions (up, down, left, right).
  7. If any of the recursive calls return true, the word exists in the grid; return true.
  8. Unmark the current cell as part of the backtracking process.
  9. If no path returns true, return false.
UML Thumbnail

Iterative Depth-First Search with Stack

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...