leetcode

Word Search

Given a 2D board and a word, find if the word exists in the grid.

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

For example,

Given board =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]

word = "ABCCED", -> returns true,

word = "SEE", -> returns true,

word = "ABCB", -> returns false.

DFS, use a visited matrix to track in this path whether the current letter is used.

public boolean exist(char[][] board, String word) {
        if (board==null || word==null) {
            return false;
        }

        if (word.isEmpty()) {
            return true;
        }

        boolean[][] visited = new boolean[board.length][board[0].length];
        for (int row=0; row<board.length; row++) {
            for (int column=0; column<board[row].length; column++) {
                if (exist(board, word, 0, row, column, visited)) {
                    return true;
                }
            }
        }

        return false;
    }

    public boolean exist(char[][] board, String word, int level, int row, int column, boolean[][] visited) {
        if (row<0||row>=board.length||column<0||column>=board[0].length) {
            return false;
        }

        if (visited[row][column]) {
            return false;
        }

        //if it is the last char in word, no need to go further
        if (level==word.length()-1) {
            return board[row][column] == word.charAt(level);
        }

        if (board[row][column]!=word.charAt(level)) {
            return false;
        }

        visited[row][column] = true;
        //check all possible directions
        boolean result = exist(board, word, level+1, row+1, column, visited) ||
                exist(board, word, level+1, row, column+1, visited) ||
                exist(board, word, level+1, row-1, column, visited) ||
                exist(board, word, level+1, row, column-1, visited);
        visited[row][column] = false;
        return result;
    }