Word Search
JavaView on GFG
Advertisement
Intuition
DFS/backtracking from each cell. Mark cells visited to avoid reuse. Unmark on backtrack.
Algorithm
- 1For each cell (i,j): DFS with index into word.
- 2At each step: check bounds, match, not visited. Mark visited. Recurse in 4 directions. Unmark.
Common Pitfalls
- •Mark cell as visited during recursion, unmark after. Check character match before recursing.
Word Search.java
Java
// Approach: Backtracking DFS on grid. For each starting cell try to match the word character by character.
// Time: O(n*m * 4^len) Space: O(len)
class Solution {
static public boolean isWordExist(char[][] mat, String word) {
int n = mat.length, m = mat[0].length;
boolean[][] visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mat[i][j] == word.charAt(0) && dfs(mat, word, i, j, 0, visited))
return true;
}
}
return false;
}
private static boolean dfs(char[][] mat, String word, int i, int j, int index, boolean[][] visited) {
if (index == word.length())
return true;
if (i < 0 || i >= mat.length || j < 0 || j >= mat[0].length || visited[i][j] || mat[i][j] != word.charAt(index))
return false;
visited[i][j] = true;
boolean found = dfs(mat, word, i + 1, j, index + 1, visited) ||
dfs(mat, word, i - 1, j, index + 1, visited) ||
dfs(mat, word, i, j + 1, index + 1, visited) ||
dfs(mat, word, i, j - 1, index + 1, visited);
visited[i][j] = false; // Backtrack
return found;
}
}Advertisement
Was this solution helpful?