public class Solution {
public boolean exist(char[][] board, String word) {
if (board == null || board.length == 0) return false;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (dfs(board, i, j, 0, word)) return true;
}
}
return false;
}
private boolean dfs(char[][] board, int i, int j, int index, String word) {
if (index == word.length()) return true;
if (i < 0 || i > board.length - 1 || j < 0 || j > board[0].length - 1 || word.charAt(index) != board[i][j]) return false;
board[i][j] = '*';
boolean flag = dfs(board, i - 1, j, index + 1, word) ||
dfs(board, i + 1, j, index + 1, word) ||
dfs(board, i, j - 1, index + 1, word) ||
dfs(board, i, j + 1, index + 1, word);
board[i][j] = word.charAt(index);
return flag;
}
}