-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch for Word.cpp
42 lines (34 loc) · 1 KB
/
Search for Word.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
// say Alhamdulillah
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool backTrack(int i, int j, int start, vector<vector<char>> &board,
string word) {
if (start == word.size())
return true;
if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size() ||
board[i][j] != word[start])
return false;
char curr = board[i][j];
board[i][j] = '#';
bool temp = backTrack(i, j - 1, start + 1, board, word) ||
backTrack(i, j + 1, start + 1, board, word) ||
backTrack(i - 1, j, start + 1, board, word) ||
backTrack(i + 1, j, start + 1, board, word);
board[i][j] = curr;
return temp;
}
bool exist(vector<vector<char>> &board, string word) {
int m = board.size();
int n = board[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (backTrack(i, j, 0, board, word)) {
return true;
}
}
}
return false;
}
};