Why does my solution say run time exceeded?


  • 1
    T

    class Solution {
    public:

    bool searchBoard(vector<vector<char>>& board, string word, string matchSoFar, int row, int col, vector<vector<bool>> visited) 
    {
        bool ret = false;
        visited[row][col] = true;
        if(matchSoFar.find(word)!=std::string::npos)
        {
            cout<<"Word found" <<matchSoFar<<endl;
            return true;
        }
        
        if(!ret && ((row-1) >= 0) && (visited[row-1][col] == false))
        {
            ret = ret || searchBoard(board,word,matchSoFar+board[row-1][col],row-1,col, visited);
        }
        if(!ret && ((row+1) < board.size())&& (visited[row+1][col] == false))
        {
            ret = ret || searchBoard(board,word,matchSoFar+board[row+1][col],row+1,col, visited);
        }
        if(!ret && ((col-1) >= 0)&& (visited[row][col-1] == false))
        {
            ret = ret || searchBoard(board,word,matchSoFar+board[row][col-1],row,col-1, visited);
        }
        if(!ret && ((col+1) < board[0].size())&& (visited[row][col+1] == false))
        {
            ret = ret || searchBoard(board,word,matchSoFar+board[row][col+1],row,col+1, visited);
        }
        
        return ret;
    }
    bool exist(vector<vector<char>>& board, string word) 
    {
        if(board.size() == 0)
            return false;
        vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size()));     
        return(searchBoard(board,word,string(1,board[0][0]),0,0, visited));
    }
    

    };


Log in to reply
 

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