# Word Search leetcode

lpy1990

## 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 =

``````[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
``````

word = "ABCCED", -> returns true, word = "SEE", -> returns true, word
= "ABCB", -> returns false.

### 回溯法

#### 代码

``````public boolean exist(char[][] board, String word) {
if (word == null || word.length() == 0) {
return true;
}
if (board == null || board.length == 0 || board[0].length == 0) {
return false;
}
boolean[][] visit = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if ( helper(board, visit, word, i, j, 0)) {
return true;
}
}
}
return false;
}
public boolean helper(char[][] board, boolean[][] visit, String word, int i, int j, int pos) {
if (pos == word.length()) {
return true;
}
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || visit[i][j] || board[i][j] != word.charAt(pos)) {
return false;
}
visit[i][j] = true;
boolean res = helper(board, visit, word, i + 1, j, pos + 1) || helper(board, visit, word, i - 1, j, pos + 1) || helper(board, visit, word, i , j - 1, pos + 1) || helper(board, visit, word, i, j + 1, pos + 1);
visit[i][j] = false;
return res;
}``````

26 声望
10 粉丝
0 条评论