public class Solution {
public boolean exist(char[][] board, String word) {
int rowLength = board.length;
int colLength = board[0].length;
for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < colLength; j++) {
if (board[i][j] == word.charAt(0)) {
if (existsHelper(board, i, j, word.substring(1))) {
return true;
}
}
}
}
return false;
}
public boolean existsHelper(char[][] board, int r, int c, String word) {
if (word.length() == 0) {
return true;
}
if (c > 0 && board[r][c - 1] == word.charAt(0)) {
char cur = board[r][c];
board[r][c] = '*';
if (existsHelper(board, r, c - 1, word.substring(1))) {
return true;
}
board[r][c] = cur;
}
if (r > 0 && board[r - 1][c] == word.charAt(0)) {
char cur = board[r][c];
board[r][c] = '*';
if (existsHelper(board, r - 1, c, word.substring(1))) {
return true;
}
board[r][c] = cur;
}
if (r < board.length - 1 && board[r + 1][c] == word.charAt(0)) {
char cur = board[r][c];
board[r][c] = '*';
if (existsHelper(board, r + 1, c, word.substring(1))) {
return true;
}
board[r][c] = cur;
}
if (c < board[0].length - 1 && board[r][c + 1] == word.charAt(0)) {
char cur = board[r][c];
board[r][c] = '*';
if (existsHelper(board, r, c + 1, word.substring(1))) {
return true;
}
board[r][c] = cur;
}
return false;
}
}
0 Comments