|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 79. Word Search |
5 |
| - * |
6 |
| - * Given a 2D board and a word, find if the word exists in the grid. |
7 |
| - * The word can be constructed from letters of sequentially adjacent cell, |
8 |
| - * where "adjacent" cells are those horizontally or vertically neighboring. |
9 |
| - * The same letter cell may not be used more than once. |
10 |
| -
|
11 |
| - For example, |
12 |
| - Given board = |
13 |
| - [ |
14 |
| - ['A','B','C','E'], |
15 |
| - ['S','F','C','S'], |
16 |
| - ['A','D','E','E'] |
17 |
| - ] |
18 |
| -
|
19 |
| - word = "ABCCED", -> returns true, |
20 |
| - word = "SEE", -> returns true, |
21 |
| - word = "ABCB", -> returns false. |
22 |
| - */ |
23 |
| - |
24 | 3 | public class _79 {
|
25 | 4 |
|
26 | 5 | public static class Solution1 {
|
27 | 6 |
|
28 |
| - public boolean exist(char[][] board, String word) { |
29 |
| - int m = board.length; |
30 |
| - int n = board[0].length; |
31 |
| - boolean[][] visited = new boolean[m][n]; |
32 |
| - for (int i = 0; i < m; i++) { |
33 |
| - for (int j = 0; j < n; j++) { |
34 |
| - if (dfs(board, visited, i, j, word, 0)) { |
35 |
| - return true; |
| 7 | + public boolean exist(char[][] board, String word) { |
| 8 | + int m = board.length; |
| 9 | + int n = board[0].length; |
| 10 | + boolean[][] visited = new boolean[m][n]; |
| 11 | + for (int i = 0; i < m; i++) { |
| 12 | + for (int j = 0; j < n; j++) { |
| 13 | + if (dfs(board, visited, i, j, word, 0)) { |
| 14 | + return true; |
| 15 | + } |
| 16 | + } |
36 | 17 | }
|
37 |
| - } |
| 18 | + return false; |
38 | 19 | }
|
39 |
| - return false; |
40 |
| - } |
41 | 20 |
|
42 |
| - final int[] dirs = new int[] {0, 1, 0, -1, 0}; |
| 21 | + final int[] dirs = new int[]{0, 1, 0, -1, 0}; |
43 | 22 |
|
44 |
| - boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) { |
45 |
| - if (index >= word.length() || word.charAt(index) != board[row][col]) { |
46 |
| - return false; |
47 |
| - } else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) { |
48 |
| - visited[row][col] = true; |
49 |
| - return true; |
50 |
| - } |
51 |
| - visited[row][col] = true;//set it to true for this case |
52 |
| - boolean result = false; |
53 |
| - for (int i = 0; i < 4; i++) { |
54 |
| - int nextRow = row + dirs[i]; |
55 |
| - int nextCol = col + dirs[i + 1]; |
56 |
| - if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) { |
57 |
| - continue; |
58 |
| - } |
59 |
| - result = dfs(board, visited, nextRow, nextCol, word, index + 1); |
60 |
| - if (result) { |
| 23 | + boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) { |
| 24 | + if (index >= word.length() || word.charAt(index) != board[row][col]) { |
| 25 | + return false; |
| 26 | + } else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) { |
| 27 | + visited[row][col] = true; |
| 28 | + return true; |
| 29 | + } |
| 30 | + visited[row][col] = true;//set it to true for this case |
| 31 | + boolean result = false; |
| 32 | + for (int i = 0; i < 4; i++) { |
| 33 | + int nextRow = row + dirs[i]; |
| 34 | + int nextCol = col + dirs[i + 1]; |
| 35 | + if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + result = dfs(board, visited, nextRow, nextCol, word, index + 1); |
| 39 | + if (result) { |
| 40 | + return result; |
| 41 | + } else { |
| 42 | + visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!! |
| 43 | + } |
| 44 | + } |
61 | 45 | return result;
|
62 |
| - } else { |
63 |
| - visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!! |
64 |
| - } |
65 | 46 | }
|
66 |
| - return result; |
67 |
| - } |
68 | 47 | }
|
69 | 48 |
|
70 | 49 | public static class Solution2 {
|
@@ -95,9 +74,9 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
|
95 | 74 | }
|
96 | 75 | visited[i][j] = true;
|
97 | 76 | if (search(board, word, i + 1, j, pos + 1)
|
98 |
| - || search(board, word, i - 1, j, pos + 1) |
99 |
| - || search(board, word, i, j + 1, pos + 1) |
100 |
| - || search(board, word, i, j - 1, pos + 1)) { |
| 77 | + || search(board, word, i - 1, j, pos + 1) |
| 78 | + || search(board, word, i, j + 1, pos + 1) |
| 79 | + || search(board, word, i, j - 1, pos + 1)) { |
101 | 80 | return true;
|
102 | 81 | }
|
103 | 82 |
|
|
0 commit comments