C++ DFS Solution || TLE explanation
For those of you who are getting TLE, eventhough the logic seems correct, make sure you pass board by reference instead of passing by value.\nUse : \n\nbool DFS
For those of you who are getting TLE, eventhough the logic seems correct, make sure you **pass board by reference** instead of passing by value.\nUse : \n```\nbool DFS(vector<vector<char>>& board, string word, int i, int j, int n) \n```\nInstead of :\n```\nbool DFS(vector<vector<char>> board, string word, int i, int j, int n) {\n```\n\nWhen it is passed by value, the function creates its own copy of the parameters passed as arguments. This means it makes a copy of board everytime the function is called.  \nTook me a good 1 hour to figure this out!\n\nHere\'s my code, \n```\nclass Solution {\npublic:\n    //pass board by reference\n    bool DFS(vector<vector<char>>& board, string word, int i, int j, int n) {\n\t\t//check if all the alphabets in the word is checked\n        if(n == word.size()) return true; \n        \n\t\t//check if i and j are out of bound or if the characters aren\'t equal\n        if(i < 0 || i >= board.size() || j < 0 || j >= board[i].size() || board[i][j] != word[n]) return false;\n        \n\t\t//mark as visited \n        board[i][j] = \'0\';\n        \n\t\t//branch out in all 4 directions\n        bool status = DFS(board, word, i + 1, j, n + 1) ||  //down\n                        DFS(board, word, i, j + 1, n + 1) ||  //right\n                        DFS(board, word, i - 1, j, n + 1) ||  //up\n                        DFS(board, word, i, j - 1, n + 1);  //left\n        \n\t\t//change the character back for other searches\n        board[i][j] = word[n];\n\t\t\n        return status;\n    }\n    \n    bool exist(vector<vector<char>>& board, string word) {\n        if(word == "") return false;\n        \n        for(int i = 0; i < board.size(); i++) \n            for(int j = 0; j < board[i].size(); j++) \n\t\t\t\t//check if the characters are equal then call DFS\n                if(board[i][j] == word[0] && DFS(board, word, i, j, 0))\n                    return true;\n        \n        return false;\n    }\n};\n```\n\nHappy coding :)
['Depth-First Search', 'C']