-
Notifications
You must be signed in to change notification settings - Fork 5
/
day-112.cpp
78 lines (53 loc) · 2.33 KB
/
day-112.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
73
74
75
76
77
78
/*
Word Search
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
*/
// DFS approach beats 95% of cpp solution
class Solution {
public:
bool isValidIndex(vector<vector<char>>& board, int row, int col) {
int rows = board.size();
int cols = board[0].size();
if ((row >= 0 && row < rows) && (col >= 0 && col < cols)) return true;
return false;
}
bool searchInGrid(vector<vector<char>>& board, int row, int col, string& word, int idx) {
if (idx == word.length() - 1) return true;
if (!isValidIndex(board, row, col) || (word[idx] != board[row][col])) return false;
char temp = board[row][col];
board[row][col] = '$';
if (isValidIndex(board, row, col - 1) && board[row][col - 1] == word[idx + 1] && searchInGrid(board, row, col - 1, word, idx + 1)) return true;
if (isValidIndex(board, row, col + 1) && board[row][col + 1] == word[idx + 1] && searchInGrid(board, row, col + 1, word, idx + 1)) return true;
if (isValidIndex(board, row - 1, col) && board[row - 1][col] == word[idx + 1] && searchInGrid(board, row - 1, col, word, idx + 1)) return true;
if (isValidIndex(board, row + 1, col) && board[row + 1][col] == word[idx + 1] && searchInGrid(board, row + 1, col, word, idx + 1)) return true;
board[row][col] = temp;
return false;
}
bool exist(vector<vector<char>>& board, string word) {
char startCharacter = word[0];
int rows = board.size();
int cols = board[0].size();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if ((board[row][col] == startCharacter) && searchInGrid(board, row, col, word, 0)) return true;
}
}
return false;
}
};