-
Notifications
You must be signed in to change notification settings - Fork 257
/
word-search.cpp
68 lines (57 loc) · 1.82 KB
/
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
// Time: O(m * n * l), l is length of the word
// Space: O(l)
class Solution {
public:
/**
* @param board: A list of lists of character
* @param word: A string
* @return: A boolean
*/
bool exist(vector<vector<char>> &board, string word) {
unordered_set<string> ret;
vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
int cur = 0;
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
if (wordSearchDFS(board, visited, word, i, j, cur)) {
return true;
}
}
}
return false;
}
bool wordSearchDFS(vector<vector<char>> &grid,
vector<vector<bool>> &visited,
string word,
int i,
int j,
int cur) {
// Invalid state.
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size()) {
return false;
}
// Not mathced or visited.
if (grid[i][j] != word[cur] || visited[i][j]) {
return false;
}
// Update current string.
++cur;
// Find the string, add to the answers.
if (cur == word.length()) {
return true;
}
// Marked as visited.
visited[i][j] = true;
// Try each direction.
const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
for (const auto& d : directions) {
if (wordSearchDFS(grid, visited, word,
i + d.first, j + d.second, cur)) {
return true;
}
}
visited[i][j] = false;
return false;
}
};