-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question18.cpp
34 lines (30 loc) · 929 Bytes
/
Question18.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
18 -> Word Search
Solution :
class Solution {
public:
int n,m;
bool solve(int ind,int i,int j,vector<vector<char>>& board,string &word){
if(ind==word.length())return true;
if(i>=n||i<0||j>=m||j<0||board[i][j]!=word[ind])return false;
// char curr=board[i][j];
board[i][j]='-1';
if(solve(ind+1,i+1,j,board,word))return true;
if(solve(ind+1,i,j+1,board,word))return true;
if(solve(ind+1,i-1,j,board,word))return true;
if(solve(ind+1,i,j-1,board,word))return true;
board[i][j]=word[ind];
return false;
}
bool exist(vector<vector<char>>& board, string word) {
n=board.size();
m=board[0].size();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(solve(0,i,j,board,word)){
return true;
}
}
}
return false;
}
};