Given a grid of letters, board, and a string, word, return true if word can be traced in the grid, and false if it can't. Each letter of the trace must be in a cell next to the last one: up, down, left or right. And the same cell can't be used twice.
Examples
Example 1:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
Example 2:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true
Example 3:
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
Explanation: the B would have to be reused.
Example 4:
Input: board = [["A","B"],["C","D"]], word = "ABDC"
Output: true
Explanation: a path may snake — right, down, left.
Constraints
1 <= rows, columns <= 6
1 <= word.length <= 15
Board and word consist of uppercase English letters.
Prerequisites
Backtracking — choose, recurse, undo. The undo is what makes the rest correct. Pruning: cutting a branch the moment it cannot lead anywhere.
How to think about it
1. Mark the Cell, Unmark on the Way Back Optimal
Intuition
Walk the grid letter by letter, mark the cells you use, and unmark them when you step back. Any cell can start the word, so try a depth-first search from each. The mark is written into the board itself, a '#', which matches no letter, so the walk can never step onto a cell it is already using. When a path dead-ends, every call returning puts its cell's letter back, so the next path finds the board as it was: a cell one path gave up on may be exactly where another path starts.
Algorithm
1. Keep the board and the word, and call dfs(r, c, 0) from every cell; the first that answers true answers true. 2. In dfs(r, c, i): i is the word's length, so every letter is matched: true. 3. Off the board: false. The cell is not the word's letter i (a marked cell never is): false. 4. Mark the cell '#', and try down, up, right and left for letter i + 1. 5. Put the cell's letter back, and return whether any of the four found the rest.
Time & Space
Time O(r·c·3^L) for an r by c board and a word of L letters: any cell can be a start, and after the first step each call has at most three new ways to go, the way back being marked. Space O(L) for the calls; the marks are in the board.
2. DFS With an Undo Optimal
Intuition
Try every cell as a starting point, then walk neighbours matching the word letter by letter. The only subtlety is the mark: a cell must be off-limits for the duration of a path and available again afterwards, because a different path may legitimately use it. The mark is what keeps a path from using a cell twice — "ABCB" is in the tests because it is what catches a missing one — and the unmark on the way out is what frees the cell for the next path.
Algorithm
1. For each cell, start a DFS at word position 0. 2. Out of bounds, already used, or a letter mismatch: fail this branch. 3. Mark the cell, try all four neighbours for the next letter. 4. Unmark before returning. 5. Reaching the word's end is success.
Time & Space
Time O(cells x 3^len) worst case — three onward directions after the first step. Space O(len) for the recursion plus the used grid.