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 }
View Code

相关文章:

  • 2021-12-26
  • 2022-12-23
  • 2021-09-09
  • 2021-10-20
  • 2021-09-16
  • 2021-10-23
  • 2021-08-25
  • 2021-11-18
猜你喜欢
  • 2021-10-15
  • 2021-10-09
  • 2022-02-18
  • 2021-07-25
  • 2021-12-22
  • 2022-02-24
  • 2021-05-27
相关资源
相似解决方案