MCPcopy Create free account
hub / github.com/chaharnishant11/CodeIn10DSA / Solution

Class Solution

Backtracking/Code/wordSearch.cpp:1–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2public:
3 bool helper(vector<vector<char>>& board,string word,int i,int j,int n,int m,int k){
4 if(k >= word.size())return true;
5 if(i<0 || i>=n || j<0 || j>=m || board[i][j]=='.' || word[k]!=board[i][j]) return false;
6 if(word.size() == 1 && word[k]==board[i][j]) return true;
7 board[i][j] = '.';
8 bool temp = false;
9 int x[4] = {0,0,-1,1};
10 int y[4] = {-1,1,0,0};
11 for(int index=0;index<4;index++){
12 temp = temp || helper(board,word,i+x[index],j+y[index],n,m,k+1);
13 }
14 board[i][j] = word[k];
15 return temp;
16 }
17
18 bool exist(vector<vector<char>>& board,string word) {
19 int n=board.size();
20 if(n==0) return false;
21 int m=board[0].size();
22 if(word.size()==0) return false;
23 for(int i=0;i<n;i++){
24 for(int j=0;j<m;j++){
25 if(word[0]==board[i][j]){
26 if(helper(board,word,i,j,n,m,0))return true;
27 }
28 }
29 }
30 return false;
31 }
32};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected