forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79. Word Search.cpp
72 lines (47 loc) · 1.7 KB
/
79. Word Search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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.
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.
Constraints:
board and word consists only of lowercase and uppercase English letters.
1 <= board.length <= 200
1 <= board[i].length <= 200
1 <= word.length <= 10^3
class Solution {
public:
bool dfs(int i, int j, vector<vector<char> > &board, int count, string word, vector<vector<bool> > &vis){
//base case
if(count==word.length()) return true;
// boundary check
if(i<0 || i>=board.size() || j<0 || j>= board[0].size() || board[i][j]!=word[count] || vis[i][j])
return false;
vis[i][j]=true;
if(dfs(i-1, j, board, count+1, word, vis)
|| dfs(i, j+1, board, count+1,word, vis)
|| dfs(i+1, j, board, count+1, word, vis)
|| dfs(i, j-1, board, count+1, word, vis))
return true;
vis[i][j]=false; // backtrack
return false;
}
bool exist(vector<vector<char>>& board, string word) {
int row=board.size();
int col=board[0].size();
vector<vector<bool> > vis(row, vector<bool> (col, false));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(board[i][j]==word[0] && dfs(i, j, board, 0, word, vis))
return true;
}
}
return false;
}
};