(char[][] board, String word)
| 3 | private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; |
| 4 | |
| 5 | public boolean exist(char[][] board, String word) { |
| 6 | int m = board.length; |
| 7 | int n = board[0].length; |
| 8 | visited = new boolean[m][n]; |
| 9 | |
| 10 | for (int i = 0; i < m; i++) { |
| 11 | for (int j = 0; j < n; j++) { |
| 12 | if (dfs(board, word, i, j, 0)) { |
| 13 | return true; |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | private boolean dfs(char[][] board, String word, int i, int j, int index) { |
| 21 | if (index == word.length()) { |