https://leetcode.com/problems/word-search/
X. https://leetcode.com/problems/word-search/discuss/27811/My-Java-solution
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.
Example:
board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] Given word = "ABCCED", return true. Given word = "SEE", return true. Given word = "ABCB", return false.https://leetcode.com/problems/word-search/discuss/27834/Simple-solution
public boolean exist(char[][] board, String word) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if(exist(board, i, j, word, 0)) return true;
}
}
return false;
}
private boolean exist(char[][] board, int x, int y, String word, int start) {
if(start >= word.length()) return true;
if(x < 0 || x >= board.length || y < 0 || y >= board[0].length) return false;
if (board[x][y] == word.charAt(start++)) {
char c = board[x][y];
board[x][y] = '#';
boolean res = exist(board, x + 1, y, word, start) || exist(board, x - 1, y, word, start) ||
exist(board, x, y + 1, word, start) || exist(board, x, y - 1, word, start);
board[x][y] = c;
return res;
}
return false;
}
https://leetcode.com/problems/word-search/discuss/27658/Accepted-very-short-Java-solution.-No-additional-space.ublic boolean exist(char[][] board, String word) {
char[] w = word.toCharArray();
for (int y=0; y<board.length; y++) {
for (int x=0; x<board[y].length; x++) {
if (exist(board, y, x, w, 0)) return true;
}
}
return false;
}
private boolean exist(char[][] board, int y, int x, char[] word, int i) {
if (i == word.length) return true;
if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
if (board[y][x] != word[i]) return false;
board[y][x] ^= 256;
boolean exist = exist(board, y, x+1, word, i+1)
|| exist(board, y, x-1, word, i+1)
|| exist(board, y+1, x, word, i+1)
|| exist(board, y-1, x, word, i+1);
board[y][x] ^= 256;
return exist;
}
X. https://leetcode.com/problems/word-search/discuss/27811/My-Java-solution
static boolean[][] visited;
public boolean exist(char[][] board, String word) {
visited = new boolean[board.length][board[0].length];
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[i].length; j++){
if((word.charAt(0) == board[i][j]) && search(board, word, i, j, 0)){
return true;
}
}
}
return false;
}
private boolean search(char[][]board, String word, int i, int j, int index){
if(index == word.length()){
return true;
}
if(i >= board.length || i < 0 || j >= board[i].length || j < 0 || board[i][j] != word.charAt(index) || visited[i][j]){
return false;
}
visited[i][j] = true;
if(search(board, word, i-1, j, index+1) ||
search(board, word, i+1, j, index+1) ||
search(board, word, i, j-1, index+1) ||
search(board, word, i, j+1, index+1)){
return true;
}
visited[i][j] = false;
return false;
}