The board is the visited set

Write # going in, put the letter back coming out.

LC 79 is the Week 3 template on a grid: the choices are the four neighbours, and choose / un-choose is writing # into the cell and restoring its letter. A cell holding # can never match a letter, so the path can't loop back on itself.

word

running now on the path, holds # letter given back the word, found

Press Next to start.

Call stack

    LC 79

    Why # works as a visited mark

    Every call starts by comparing its cell with the letter it needs. A cell on the current path holds #, which matches no letter, so a call that steps back onto the path fails at once. Pick "ABA" on [A B]: the walk reaches B, looks for an A, finds only the one it started from, and that one says #.

    On the way out, a failed branch writes word[idx] back. That is always the right letter: the call only got past its first check because the cell held exactly word[idx]. Pick "ABCB" and watch the cells turn green as each dead end hands its letter back.

    Whatever a call marks, it unmarks before returning false.

    What happens on success

    When a neighbour reports true, the call returns at once, before the restore line. So after a successful search the found path still holds # in every cell: step to the end of "ABCCED" and the board shows it. LeetCode only checks the bool, so the solution is accepted, but a caller that reuses the board would find it damaged. After a failed search the board is always back to how it started.