Problem : LC 79
error in the picture:
my code:
class Solution {
private:
bool func(vector<vector<char> > board, string word, int ind,
int i, int j){
int n = board.size();
int m = board[0].size();
if(ind == word.size()) return true;
if (i < 0 || i >= n || j < 0 || j >= m ||
board[i][j] != word[ind] || board[i][j]== ' ')
return false;
// we checked it so we replace it with empty element so that we dont check it again
board[i][j] = ' ';
bool ans = func(board, word, ind + 1, i - 1, j) || //top
func(board, word, ind + 1, i + 1, j) || //bottom
func(board, word, ind + 1, i, j - 1) || //left
func(board, word, ind + 1, i, j + 1); //right
return ans;
}
public:
bool exist(vector<vector<char> >& board, string word) {
//your code goes here
int n = board.size();
int m = board[0].size();
for(int i = 0; i<n; i++){
for(int j=0; j<m; j++){
if(word[0] == board[i][j]){
if(func(board, word, 0, i, j) == true){
return true;
}
}
}
}
return false;
}
};
case on which am getting the error:
board =
[["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"],["A","A","A","A","A","A"]]
word =
"AAAAAAAAAAAAAAB"