clueless coding // TODO: be smarter

LeetCode Word Search: CPP Solution



TL;DR Code Solution



bool dfs(vector<vector<char>>& board, string word, int i, int j, int index) {

    if (index == word.size()) {
        return true;
    }

    if (i < 0 || j < 0 || i >= board.size() || j >= board[0].size() || board[i][j] != word[index]) {
        return false;
    }

    char temp = board[i][j];
    board[i][j] = '1';
    if (dfs(board, word, i + 1, j, index + 1) || 
        dfs(board, word, i - 1, j, index + 1) || 
        dfs(board, word, i, j + 1, index + 1) ||
        dfs(board, word, i, j - 1, index + 1))
        return true;
    board[i][j] = temp;

    return false;

}

bool exist(vector<vector<char>>& board, string word) {

    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[0].size(); j++) {
            if (dfs(board, word, i, j, 0)) {
                return true;
            }    
        }
    }
    return false;
}