Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example:
board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] Given word = "ABCCED", return true. Given word = "SEE", return true. Given word = "ABCB", return false.
Constraints:
board
andword
consists only of lowercase and uppercase English letters.1 <= board.length <= 200
1 <= board[i].length <= 200
1 <= word.length <= 10^3
Related Topics:
Array, Backtracking
Similar Questions:
// OJ: https://leetcode.com/problems/word-search/
// Author: github.com/lzl124631x
// Time: O(MN * 4^K)
// Space: O(K)
class Solution {
int M, N, dirs[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
bool dfs(vector<vector<char>> &A, string &word, int start, int x, int y) {
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] != word[start]) return false;
if (start + 1 == word.size()) return true;
char c = A[x][y];
A[x][y] = '\0';
for (auto &dir : dirs) {
if (dfs(A, word, start + 1, x + dir[0], y + dir[1])) return true;
}
A[x][y] = c;
return false;
}
public:
bool exist(vector<vector<char>>& A, string word) {
M = A.size(), N = A[0].size();
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (dfs(A, word, 0, i, j)) return true;
}
}
return false;
}
};