Word Search


Word Search – LeetCode


Problem Statement

Given an m x n 2D board and a word, determine if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.


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


Constraints


Intuition

We need to search for the given word in a matrix such that:

This naturally leads to a backtracking solution where we:


Approach (Backtracking)

  1. For each cell in the grid, if the character matches the first character of the word:

    • Call a recursive function to check if the rest of the word can be found starting from that cell.
  2. The recursive function (find) checks:

    • If the current cell is out of bounds or already visited.

    • If the character at the current cell matches the current character in the word.

    • If we’ve reached the end of the word (idx == word.length()).

  3. Temporarily mark the cell as visited (e.g., with a *) and explore its 4 neighbors.

  4. Restore the cell (backtrack) after exploring.


Code (Java)

class Solution {
    public boolean exist(char[][] board, String word) {
        int m = board.length;
        int n = board[0].length;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (find(i, j, m, n, word, 0, board)) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean find(int i, int j, int m, int n, String word, int idx, char[][] board) {
        if (idx == word.length()) return true;

        if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] != word.charAt(idx)) {
            return false;
        }

        char ch = board[i][j];
        board[i][j] = '*'; // mark visited

        boolean left = find(i, j - 1, m, n, word, idx + 1, board);
        boolean up = find(i - 1, j, m, n, word, idx + 1, board);
        boolean right = find(i, j + 1, m, n, word, idx + 1, board);
        boolean down = find(i + 1, j, m, n, word, idx + 1, board);

        board[i][j] = ch; // backtrack

        return left || up || right || down;
    }
}

Time and Space Complexity


Dry Run

Input:

board = [["A","B","C","E"],
         ["S","F","C","S"],
         ["A","D","E","E"]]
word = "ABCCED"
  1. Start at (0,0): matches A

  2. Move to (0,1): matches B

  3. Move to (0,2): matches C

  4. Move to (1,2): matches C

  5. Move to (1,1): matches E → incorrect

  6. Backtrack and try (2,2): matches E

  7. Move to (2,1): matches D → complete

Returns true.


Conclusion

This problem is a textbook application of DFS with backtracking in a 2D matrix. The use of in-place marking and recursive exploration of valid paths highlights how backtracking works efficiently in grid search problems.