JS | DFS + Backtracking | Beats 88% | Clean Code
DFS + Backtracking will take you far\n\n\nconst exist = function(board, word) {\n    const n = board.length, m = board[0].length;\n    if (word.length < 1) retu
DFS + Backtracking will take you far\n\n```\nconst exist = function(board, word) {\n    const n = board.length, m = board[0].length;\n    if (word.length < 1) return false;\n\n    const dfs = (i, j, pos) => {\n        if (i === n || i < 0 || j === m || j < 0 || board[i][j] !== word[pos]) return false;\n        if (pos === word.length-1) return true;\n        board[i][j] = "."; // choose this elem so we don\'t find it again\n        const found = \n            dfs(i+1,j, pos+1) || // check every direction and see if any of them return a match\n            dfs(i-1,j, pos+1) ||\n            dfs(i,j+1, pos+1) ||\n            dfs(i,j-1, pos+1);\n\n        board[i][j] = word[pos]; // unchoose element\n        return found;\n    };\n    \n    for (let i=0;i<n;i++) {\n        for (let j=0;j<m;j++) {\n            if (board[i][j] === word[0]) {\n                const match = dfs(i,j, 0);\n                if (match) return true;\n            }\n        }\n    }\n\n    return false;\xA0\n};\n```
['Backtracking', 'Depth-First Search', 'Recursion', 'JavaScript']