forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.java
More file actions
86 lines (80 loc) · 2.34 KB
/
WordSearch.java
File metadata and controls
86 lines (80 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package backtracking;
/**
* Created by PRADHANG on 4/13/2017.
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.
For example,
Given board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
*/
public class WordSearch
{
private static final int[] R = {0,0,1,-1};
private static final int[] C = {1,-1,0,0};
private static boolean[][] visited;
private static int length = 0, N, M;
/**
* Main method
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
char[][] board = {
{'A'}
};
System.out.println(new WordSearch().exist(board, "A"));
}
public boolean exist(char[][] board, String word)
{
N = board.length;
M = board[0].length;
if(N * M < word.length()) return false;
visited = new boolean[N][M];
length = word.length();
for (int i = 0 ; i < N; i ++)
{
for (int j = 0 ; j < M; j ++)
{
if(board[i][j] == word.charAt(0))
{
if(dfs(i, j, board, word, 1)) return true;
visited[i][j] = false;
}
}
}
return false;
}
private boolean dfs(int r, int c, char[][] board, String word, int pos)
{
if(pos < length)
{
visited[r][c] = true;
for(int i = 0; i < 4; i ++)
{
int newR = r + R[i];
int newC = c + C[i];
if(newR >= 0 && newR < N && newC >= 0 && newC < M)
{
if(!visited[newR][newC])
{
if(board[newR][newC] == word.charAt(pos))
{
if(dfs(newR, newC, board, word, pos + 1)) return true;
visited[newR][newC] = false;
}
}
}
}
}
else return true;
return false;
}
}