DFS / Backtracking in C++, Js & Python
C++\ncpp\nclass   Solution\n{\n    public:\n\n    bool    exist(vector<vector<char>>& board, string word)\n    {\n            int     rows, cols;\n\n           
# C++\n```cpp\nclass   Solution\n{\n    public:\n\n    bool    exist(vector<vector<char>>& board, string word)\n    {\n            int     rows, cols;\n\n            if (!board.size())\n\t\t    return (false);\n            rows = (int) board.size();\n            cols = (int) board[0].size();\n            for (int i = 0; i < rows; i++)\n                for (int j = 0; j < cols; j++)\n                    if (dfs(board, word, 0, i, j))\n\t\t\t    return (true);\n            return (false);\n    }\n\n    bool    dfs(vector<vector<char>> &board, string &word, int i, int x, int y)\n    {\n            int     rows, cols;\n            bool    found;\n            char    c;\n\n            if (!board.size())\n\t\t    return (false);\n            rows = (int) board.size();\n            cols = (int) board[0].size();\n            if (x < 0 || x == rows || y < 0 || y == cols || word[i] != board[x][y])\n                return (false);\n            if (i == (int) word.length() - 1)\n                return (true);\n            c = board[x][y];\n            board[x][y] = 0;\n            found = dfs(board, word, i + 1, x + 1, y) ||\n                    dfs(board, word, i + 1, x - 1, y) ||\n                    dfs(board, word, i + 1, x, y + 1) ||\n                    dfs(board, word, i + 1, x, y - 1);\n            board[x][y] = c;\n            return (found);\n    }\n};\n```\n# Js\n```js\nvar exist = function(board, word) {\n    let i = -1\n    while (++i < board.length) {\n        let j = -1\n        while (++j < board[0].length) {\n            if (dfs(board, word, 0, i, j))\n                return true\n        }\n    }\n    return false\n};\n\nvar dfs = function(board, word, index, x, y) {\n    if (index == word.length)\n        return true\n    let c = board[0].length\n    let r = board.length\n    if (x < 0 || y < 0 || x > r - 1 || y > c - 1 || board[x][y] != word[index])\n        return false\n    board[x][y] = \'#\'\n    let isFound = (\n        dfs(board, word, index + 1, x + 1, y) ||\n        dfs(board, word, index + 1, x - 1, y) ||\n        dfs(board, word, index + 1, x, y + 1) ||\n        dfs(board, word, index + 1, x, y - 1)\n    )\n    board[x][y] = word[index]\n    return isFound\n}\n```\n# Python\n```py\nclass Solution:\n    def exist(self, board: List[List[str]], word: str) -> bool:\n        for x in range(len(board)):\n            for y in range(len(board[0])):\n                if self.DFS(board, word, 0, x, y):\n                    return True\n        return False\n\n    def DFS(self, board: List[List[str]], word:str, index: int, x: int, y: int) -> bool:\n        if index == len(word):\n            return True\n        c = len(board[0])\n        r = len(board)\n        if x < 0 or y < 0 or x > r - 1 or y > c - 1 or word[index] != board[x][y] :\n            return False\n        board[x][y] = chr(0)\n        found = (\n            self.DFS(board, word, index + 1, x + 1, y) or\n            self.DFS(board, word, index + 1, x - 1, y) or\n            self.DFS(board, word, index + 1, x, y + 1) or\n            self.DFS(board, word, index + 1, x, y - 1))\n        board[x][y] = word[index]\n        return found\n```\n# C\n```c\nbool    solve(char **board, char *word, int i, int x, int y, int r, int c);\n\nbool    exist(char** board, int boardSize, int* boardColSize, char * word)\n{\n        if (!board || !boardSize || !boardColSize) return (false);\n        if (!word)  return (true);\n        for (int i = 0; i < boardSize; i++)\n            for (int j = 0; j < boardColSize[0]; j++)\n                if (solve(board, word, 0, i, j, boardSize, boardColSize[0]))\n                    return (true);\n        return (false);\n}\n\nbool    solve(char **board, char *word, int i, int x, int y, int r, int c)\n{\n        bool    found;\n        char    temp;\n\n        if (x < 0 || x == r || y < 0 || y == c || word[i] != board[x][y])\n\t\t    return (false);\n        if (i == strlen(word) - 1)\n\t\t    return (true);\n        temp = board[x][y];\n        board[x][y] = 0;\n        found = solve(board, word, i + 1, x + 1, y, r, c) ||\n                solve(board, word, i + 1, x - 1, y, r, c) ||\n                solve(board, word, i + 1, x, y + 1, r, c) ||\n                solve(board, word, i + 1, x, y - 1, r, c);\n        board[x][y] = temp;\n        return (found);\n}\n```
['C', 'Python', 'C++', 'JavaScript']