Skip to content

Instantly share code, notes, and snippets.

@JyotinderSingh
Created June 30, 2020 09:38
Show Gist options
  • Save JyotinderSingh/39ede85f47fb66c5ed7d86d72b1d20e7 to your computer and use it in GitHub Desktop.
Save JyotinderSingh/39ede85f47fb66c5ed7d86d72b1d20e7 to your computer and use it in GitHub Desktop.
Word Search (LeetCode) | O(1) Space Algorithm Explanation
class Solution
{
public:
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 (board[i][j] == word[0])
{
if (dfs(board, word, 0, i, j))
{
return true;
}
}
}
}
return false;
}
bool dfs(vector<vector<char>> &board, string &word, int idx, int row, int col)
{
if (row < 0 || row >= board.size() || col < 0 || col >= board[0].size() || board[row][col] != word[idx])
{
return false;
}
if (idx == word.size() - 1)
{
return true;
}
char curr = board[row][col];
board[row][col] = '.';
int moveX[] = {0, 0, 1, -1}, moveY[] = {1, -1, 0, 0};
for (int i = 0; i < 4; ++i)
{
if (dfs(board, word, idx + 1, row + moveY[i], col + moveX[i]))
{
return true;
}
}
board[row][col] = curr;
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment