-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathWordSearch.java
75 lines (69 loc) · 1.84 KB
/
WordSearch.java
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
// Must change the class name to "Solution" in LeetCode
class WordSearch {
private String word;
private int m;
private int n;
private boolean[][] vis;
private char[][] board;
private final int MOVEX[] = { 1, 0, -1, 0 };
private final int MOVEY[] = { 0, -1, 0, 1 };
private final int MOVEN = 4;
/**
Find word using backtracking and DFS.
*/
private boolean find( int row, int col )
{
return find( row, col, 0 );
}
private boolean find( int row, int col, int idx )
{
if ( idx >= word.length() )
{
return true;
}
if ( row < 0 || m <= row || col < 0 || n <= col )
{
return false;
}
if ( !vis[row][col] && board[row][col] == word.charAt(idx) )
{
vis[row][col] = true;
for ( int i = 0; i<MOVEN; i++ )
{
int newRow = row + MOVEX[i];
int newCol = col + MOVEY[i];
if ( find( newRow, newCol, idx+1 ) )
{
return true;
}
}
vis[row][col] = false;
}
return false;
}
public boolean exist(char[][] board, String word) {
this.board = board;
this.word = word;
this.m = board.length;
this.n = board[0].length;
this.vis = new boolean[m][n];
for ( int row=0; row<m; row++ )
{
for ( int col=0; col<n; col++ )
{
vis[row][col] = false;
}
}
for ( int row=0; row<m; row++ )
{
for ( int col=0; col<n; col++ )
{
if ( find( row, col ) )
{
return true;
}
}
}
return false;
}
}