# [help] why me code is TLE with 85/87 cases pass

• this is my code

``````class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if(word.empty())return true;
if(board.empty())return false;
m=board.size();
n=board[0].size();
vector<vector<bool>> isUsed(m,vector<bool>(n,false));
int sz=word.length();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(board[i][j]==word[0]){
isUsed[i][j]=true;
search(board,isUsed,i,j,word,1);
isUsed[i][j]=false;
if(flag==true)return flag;
}
}
}
return flag;
}
private:
int m=0,n=0;
bool flag=false;
void search(vector<vector<char>>&board,vector<vector<bool>>&isUsed,int x,int y,string word,int order){
if(order==word.size()){flag=true;return;}
if(y>0&&board[x][y-1]==word[order]&&(isUsed[x][y-1]==false)){
isUsed[x][y-1]=true;
search(board,isUsed,x,y-1,word,order+1);
isUsed[x][y-1]=false;
}
if(y<n-1&&board[x][y+1]==word[order]&&(isUsed[x][y+1]==false)){
isUsed[x][y+1]=true;
search(board,isUsed,x,y+1,word,order+1);
isUsed[x][y+1]=false;
}
if(x>0&&board[x-1][y]==word[order]&&(isUsed[x-1][y]==false)){
isUsed[x-1][y]=true;
search(board,isUsed,x-1,y,word,order+1);
isUsed[x-1][y]=false;
}
if(x<m-1&&board[x+1][y]==word[order]&&(isUsed[x+1][y]==false)){
isUsed[x+1][y]=true;
search(board,isUsed,x+1,y,word,order+1);
isUsed[x+1][y]=false;
}
}
};
``````

my code could pass 85/87 cases and I am confused with why this code will be TLE , what wrong with it? Could anyone give me a favor, thanks a lot

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.