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 =
[
["ABCE"],
["SFCS"],
["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
Solution1:
这个题目基本上就是DFS喽。然后注意一下递归回溯的问题。我们可以建设一个boolean的数组来记录访问过的值。在return false之前,我们应该把之前置
过的标记位置回来. 时间复杂度: http://www1.mitbbs.ca/article_t1/JobHunting/32524193_32524299_2.html
time 复杂度是m*n*4^(k-1). 也就是m*n*4^k.
m X n is board size, k is word size.
recuision最深是k层,recursive部分空间复杂度应该是O(k) + O(m*n)(visit array)
1 package Algorithms.dfs; 2 3 public class Exist { 4 public boolean exist(char[][] board, String word) { 5 if (board == null || word == null 6 || board.length == 0 || board[0].length == 0) { 7 return false; 8 } 9 10 int rows = board.length; 11 int cols = board[0].length; 12 13 boolean[][] visit = new boolean[rows][cols]; 14 15 // i means the index. 16 for (int i = 0; i < rows; i++) { 17 for (int j = 0; j < cols; j++) { 18 // dfs all the characters in the matrix 19 if (dfs(board, i, j, word, 0, visit)) { 20 return true; 21 } 22 } 23 } 24 25 return false; 26 } 27 28 public boolean dfs(char[][] board, int i, int j, String word, int wordIndex, boolean[][] visit) { 29 int rows = board.length; 30 int cols = board[0].length; 31 32 int len = word.length(); 33 if (wordIndex >= len) { 34 return true; 35 } 36 37 // the index is out of bound. 38 if (i < 0 || i >= rows || j < 0 || j >= cols) { 39 return false; 40 } 41 42 // the character is wrong. 43 if (word.charAt(wordIndex) != board[i][j]) { 44 return false; 45 } 46 47 // 不要访问访问过的节点 48 if (visit[i][j] == true) { 49 return false; 50 } 51 52 visit[i][j] = true; 53 54 // 递归 55 // move down 56 if (dfs(board, i + 1, j, word, wordIndex + 1, visit)) { 57 return true; 58 } 59 60 // move up 61 if (dfs(board, i - 1, j, word, wordIndex + 1, visit)) { 62 return true; 63 } 64 65 // move right 66 if (dfs(board, i, j + 1, word, wordIndex + 1, visit)) { 67 return true; 68 } 69 70 // move left 71 if (dfs(board, i, j - 1, word, wordIndex + 1, visit)) { 72 return true; 73 } 74 75 // 回溯 76 visit[i][j] = false; 77 return false; 78 } 79 }
Solution2:
与解1是一样的,但我们可以省掉O(m*n)的空间复杂度。具体的作法是:在进入DFS后,把访问过的节点置为'#',访问完毕之后再置回来即可。
1 /* 2 * Solution 2: 可以省掉一个visit的数组 3 * */ 4 public boolean exist(char[][] board, String word) { 5 if (board == null || word == null 6 || board.length == 0 || board[0].length == 0) { 7 return false; 8 } 9 10 int rows = board.length; 11 int cols = board[0].length; 12 13 // i means the index. 14 for (int i = 0; i < rows; i++) { 15 for (int j = 0; j < cols; j++) { 16 // dfs all the characters in the matrix 17 if (dfs(board, i, j, word, 0)) { 18 return true; 19 } 20 } 21 } 22 23 return false; 24 } 25 26 public boolean dfs(char[][] board, int i, int j, String word, int wordIndex) { 27 int rows = board.length; 28 int cols = board[0].length; 29 30 int len = word.length(); 31 if (wordIndex >= len) { 32 return true; 33 } 34 35 // the index is out of bound. 36 if (i < 0 || i >= rows || j < 0 || j >= cols) { 37 return false; 38 } 39 40 // the character is wrong. 41 if (word.charAt(wordIndex) != board[i][j]) { 42 return false; 43 } 44 45 // mark it to be '#', so we will not revisit this. 46 board[i][j] = '#'; 47 48 // 递归 49 // move down 50 if (dfs(board, i + 1, j, word, wordIndex + 1)) { 51 return true; 52 } 53 54 // move up 55 if (dfs(board, i - 1, j, word, wordIndex + 1)) { 56 return true; 57 } 58 59 // move right 60 if (dfs(board, i, j + 1, word, wordIndex + 1)) { 61 return true; 62 } 63 64 // move left 65 if (dfs(board, i, j - 1, word, wordIndex + 1)) { 66 return true; 67 } 68 69 // 回溯 70 board[i][j] = word.charAt(wordIndex); 71 return false; 72 }
December 16th, 2014 重写,简洁一点点,11分钟1次AC.
其实这里的回溯有一个点要注意,就是当dfs返回true时,我们并没有回溯。原因是这时整个dfs就结束了,我们也就不需要再回溯。否则一般的递归/回溯结构
这里是需要先回溯再返回的。
1 public class Solution { 2 public boolean exist(char[][] board, String word) { 3 if (board == null || board.length == 0 || board[0].length == 0) { 4 return false; 5 } 6 7 int rows = board.length; 8 int cols = board[0].length; 9 for (int i = 0; i < rows; i++) { 10 for (int j = 0; j < cols; j++) { 11 // Check if there is a word begin from i,j. 12 if (dfs(board, word, i, j, 0)) { 13 return true; 14 } 15 } 16 } 17 18 return false; 19 } 20 21 public boolean dfs(char[][] board, String word, int i, int j, int index) { 22 int len = word.length(); 23 if (index >= len) { 24 return true; 25 } 26 27 // Check the input parameter. 28 if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) { 29 return false; 30 } 31 32 // If the current character is right. 33 if (word.charAt(index) != board[i][j]) { 34 return false; 35 } 36 37 char tmp = board[i][j]; 38 board[i][j] = '#'; 39 40 // recursion. 41 if (dfs(board, word, i + 1, j, index + 1) 42 || dfs(board, word, i - 1, j, index + 1) 43 || dfs(board, word, i, j + 1, index + 1) 44 || dfs(board, word, i, j - 1, index + 1) 45 ) { 46 return true; 47 } 48 49 // backtrace. 50 board[i][j] = tmp; 51 return false; 52 } 53 }
虽然前面的方法可以AC,但考虑到最好不要更改入参的值,所以在return true前,我们还是加一个回溯代码。
1 public class Solution { 2 public boolean exist(char[][] board, String word) { 3 if (board == null || board.length == 0 || board[0].length == 0) { 4 return false; 5 } 6 7 int rows = board.length; 8 int cols = board[0].length; 9 for (int i = 0; i < rows; i++) { 10 for (int j = 0; j < cols; j++) { 11 // Check if there is a word begin from i,j. 12 if (dfs(board, word, i, j, 0)) { 13 return true; 14 } 15 } 16 } 17 18 return false; 19 } 20 21 public boolean dfs(char[][] board, String word, int i, int j, int index) { 22 int len = word.length(); 23 if (index >= len) { 24 return true; 25 } 26 27 // Check the input parameter. 28 if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) { 29 return false; 30 } 31 32 // If the current character is right. 33 if (word.charAt(index) != board[i][j]) { 34 return false; 35 } 36 37 char tmp = board[i][j]; 38 board[i][j] = '#'; 39 40 boolean ret = dfs(board, word, i + 1, j, index + 1) 41 || dfs(board, word, i - 1, j, index + 1) 42 || dfs(board, word, i, j + 1, index + 1) 43 || dfs(board, word, i, j - 1, index + 1); 44 45 // backtrace. 46 board[i][j] = tmp; 47 return ret; 48 } 49 }
GitHub代码: