Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Backtracking

Word Search

MEDIUM✓ CHIP-TIMEDLC #79 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Starting from any cell, snake through a grid to neighbouring cells (up, down, left, right), spelling out a word one letter per cell without reusing a cell in the same path — determine whether the word can be traced this way.

A grid of letters and a target word arrive together. Starting from any single cell, the drill is to snake through the grid — one step at a time to a cell directly above, below, left, or right — spelling the word out letter by letter.

A path may never step onto the same cell twice while tracing one attempt, though a cell can be revisited by a different attempt starting elsewhere. The only question to answer is whether some starting cell and some path spells the whole word.

Diagonal moves don't count as adjacency, and the word must be spelled in full — a path that matches only a prefix or stalls partway through doesn't count as a success.

  • grids stay modest, typically no more than a few hundred cells
  • the word length is generally under twenty characters
  • a single path may never reuse the same cell twice
  • only up/down/left/right moves count as adjacent
EX 01
board = ["CAT", "AAT", "TTT"] · word = "CAT"
→ true
straight path down the first column
EX 02
board = ["CAT", "AAT", "TTT"] · word = "CATTT"
→ true
longer path winding through distinct T cells
EX 03
board = ["CAT", "AAT", "TTT"] · word = "CATS"
→ false
letter S doesn't exist anywhere in the board

The hintstake only what you need

HINT 1 THE NUDGE

The word can start anywhere, so a full solution has to try every cell as a starting point — and once inside a path, a cell already stepped on can't be revisited in that same attempt.

HINT 2 THE STRUCTURE

Marking 'visited' doesn't need a separate structure — the grid itself is free to write into. Overwrite a cell you step on with a character the word can never contain, then put the original letter back before trying a different direction.

HINT 3 ONE STEP FROM THE ANSWER

DFS from a starting cell: if the current cell matches the current letter, temporarily blank it, recurse into all four neighbours for the next letter, then restore it. Success is reaching one past the last letter.

Coach's boardthe pattern, step by step

THE IN-PLACE SNAKEPATTERN · DFS, MARK IN PLACEboard = ["CAT","AAT","TTT"] · word = "CAT"
C
A
T
A
A
T
T
T
T
STEP 1

Word CAT on a 3x3 board. Try every starting cell — begin at row 0, column 0, which holds 'C'.

STEP 1 / 6 · ← → WORK TOO

The splitstwo paces, two languages

grind/word-search.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        rows, cols = len(board), len(board[0])

        def dfs(r, c, i):
            if i == len(word):
                return True
            if r < 0 or r >= rows or c < 0 or c >= cols or board[r][c] != word[i]:
                return False
            temp = board[r][c]
            board[r][c] = "#"  # blank it in place — the grid IS the visited set
            found = (
                dfs(r + 1, c, i + 1)
                or dfs(r - 1, c, i + 1)
                or dfs(r, c + 1, i + 1)
                or dfs(r, c - 1, i + 1)
            )
            board[r][c] = temp
            return found

        for r in range(rows):
            for c in range(cols):
                if dfs(r, c, 0):
                    return True
        return False
TIME O(M·N·4ᴸ)SPACE O(L)PYTHON · RACE PACE · 25 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED