forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-search.cpp
67 lines (56 loc) · 1.79 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
// 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 curr = 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, curr)) {
return true;
}
}
}
return false;
}
bool wordSearchDFS(vector<vector<char>> &grid,
vector<vector<bool>> &visited,
string word,
int i,
int j,
int curr) {
// 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[curr] || visited[i][j] ) {
return false;
}
// Update current string.
++curr;
// Find the string, add to the answers.
if (curr == word.length()) {
return true;
}
// Marked as visited.
visited[i][j] = true;
// Try each direction.
vector<pair<int, int>> direction{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
for (int k = 0; k < 4; ++k) {
if (wordSearchDFS(grid, visited, word,
i + direction[k].first, j + direction[k].second, curr)) {
return true;
}
}
visited[i][j] = false;
return false;
}
};