-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path30.wordSearchII.cpp
40 lines (33 loc) · 1.28 KB
/
30.wordSearchII.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
class Solution {
private:
//timepass naive founder in the grid..
bool found(vector<vector<char>> &board, string &word, int i, int j, int count){
//base case..
if(count == word.length()) return true;
//get out of here..
if(i <0 || j < 0 || j >= board.size() || i >= board.size() || word[count] != board[i][j])
return false;
count++;
char temp = board[i][j];
board[i][j] = '*';
bool present = found(board, word, i + 1, j , count) || found(board, word, i - 1, j, count) || found(board, word, i, j + 1, count) || found(board, word, i, j-1, count);
board[i][j] = temp;
return present;
}
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
if(board.size() == 0 || words.size() == 0) return {};
int m = board.size(), n = board[0].size();
vector<string> result;
for(auto word: words){
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(board[i][j] == word[0] && found(board, word, i, j, 0)){
result.push_back(word);
}
}
}
}
return result;
}
};