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

Class Solution

Backtracking/Homework/wordSearch.cpp:1–31  ·  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 bool exist(vector<vector<char>>& board, string word){
18 int n=board.size();
19 if(n==0) return false;
20 int m=board[0].size();
21 if(word.size()==0) return false;
22 for(int i=0;i<n;i++){
23 for(int j=0;j<m;j++){
24 if(word[0]==board[i][j]){
25 if(helper(board,word,i,j,n,m,0)) return true;
26 }
27 }
28 }
29 return false;
30 }
31};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected