LeetCode - Word Search

Word Search

2014.2.26 23:59

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.

Solution:

  Simple problem of DFS.

  Total time complexity is O(n^2 * len(word)). Space complexity is O(n^2).

Accepted code:

 1 // 1CE, 1AC, very smooth.
 2 class Solution {
 3 public:
 4     bool exist(vector<vector<char> > &board, string word) {
 5         n = (int)board.size();
 6         if (n == 0) {
 7             return false;
 8         }
 9         m = (int)board[0].size();
10         word_len = (int)word.length();
11         
12         if (word_len == 0) {
13             return true;
14         }
15         
16         int i, j;
17         for (i = 0; i < n; ++i) {
18             for (j = 0; j < m; ++j) {
19                 if(dfs(board, word, i, j, 0)) {
20                     return true;
21                 }
22             }
23         }
24         return false;
25     }
26 private:
27     int n, m;
28     int word_len;
29     
30     bool dfs(vector<vector<char> > &board, string &word, int x, int y, int idx) {
31         if (x < 0 || x > n - 1 || y < 0 || y > m - 1) {
32             return false;
33         }
34         
35         if (board[x][y] < 'A' || board[x][y] != word[idx]) {
36             // already searched here
37             // letter mismatch here
38             return false;
39         }
40         
41         bool res;
42         if (idx == word_len - 1) {
43             // reach the end of word, success
44             return true;
45         } else {
46             // up
47             board[x][y] -= 'A';
48             res = dfs(board, word, x - 1, y, idx + 1);
49             board[x][y] += 'A';
50             if (res) {
51                 return true;
52             }
53             
54             // down
55             board[x][y] -= 'A';
56             res = dfs(board, word, x + 1, y, idx + 1);
57             board[x][y] += 'A';
58             if (res) {
59                 return true;
60             }
61             
62             // left
63             board[x][y] -= 'A';
64             res = dfs(board, word, x, y - 1, idx + 1);
65             board[x][y] += 'A';
66             if (res) {
67                 return true;
68             }
69             
70             // right
71             board[x][y] -= 'A';
72             res = dfs(board, word, x, y + 1, idx + 1);
73             board[x][y] += 'A';
74             if (res) {
75                 return true;
76             }
77         }
78         // all letters will be within [A-Z], thus I marked a position as 'searched' by setting them to an invalid value.
79         // we have to restore the value when the DFS is done, so their values must still be distiguishable.
80         // therefore, I used an offset value of 'A'.
81         // this tricky way is to save the extra O(n * m) space needed as marker array.
82         
83         return false;
84     }
85 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3570506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值