-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord Search II.cpp
72 lines (64 loc) · 2.32 KB
/
Word Search II.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
// 212. Word Search II
// Hard. Faster than 86.13%
class Solution {
public:
// store the direction of movements in the row and column direction
const int move_x[4] = {0, 1, 0, -1};
const int move_y[4] = {1, 0, -1, 0};
// function to check whether the particular cell is safe in the board
// a dot ('.') indicated that the cell has already been visited
bool isSafe(int x, int y, vector<vector<char>>& board)
{
return (x >= 0 && x < board.size() && y >= 0 && y < board[0].size() && board[x][y] != '.');
}
// function to check the words presence using DFS
void dfs(int i, int j, vector<vector<char>>& board, bool &found, string str, int pos, vector<string> &res)
{
// if the last index is reached (pos == str.size() signifies that the control has reached after the last character)
// then it means we have achieved the desired result
if(pos == str.size())
{
res.push_back(str);
found = true;
return;
}
if(!isSafe(i, j, board) || str[pos] != board[i][j])
return;
char ch = board[i][j];
board[i][j] = '.';
for(int k = 0; k < 4; ++k)
{
int nextI = i + move_x[k], nextJ = j + move_y[k];
dfs(nextI, nextJ, board, found, str, pos + 1, res);
// if string is found then no need to iterate further, so break out
if(found)
break;
}
// backtrack
board[i][j] = ch;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
bool found;
vector<string> res;
for(string str : words)
{
found = false;
for(int i = 0; i < board.size(); ++i)
{
for(int j = 0; j < board[0].size(); ++j)
{
if(str[0] == board[i][j])
{
dfs(i, j, board, found, str, 0, res);
}
// if a word is found then there is no need to find more indices for that
if(found)
break;
}
if(found)
break;
}
}
return res;
}
};