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.
Copy [
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
Copy word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
Copy class Solution {
static boolean[][] visited;
boolean dfs(char[][] board, int i, int j, String word, int pos){
if(pos == word.length()){
return true;
}
if(i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != word.charAt(pos) || visited[i][j]){
return false;
}
visited[i][j] = true;
if(dfs(board,i + 1,j, word, pos + 1)
|| dfs(board,i,j + 1, word, pos + 1)
|| dfs(board,i - 1,j, word, pos + 1)
|| dfs(board,i,j - 1, word, pos + 1)){
return true;
}
visited[i][j] = false;
return false;
}
public boolean exist(char[][] board, String word) {
int n = board.length;
int m = board[0].length;
visited = new boolean[n][m];
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
if((word.charAt(0) == board[i][j]) && dfs(board,i,j, word, 0)){
return true;
}
}
}
return false;
}
}