-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordSearch.java
More file actions
110 lines (87 loc) · 3.2 KB
/
WordSearch.java
File metadata and controls
110 lines (87 loc) · 3.2 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package graph;
import java.util.Deque;
import java.util.LinkedList;
/**
* Description: https://leetcode.com/problems/word-search
* Difficulty: Medium
* Time complexity: O(m * n * 3^word.length)
* Space complexity: O(m * n)
*/
public class WordSearch {
private int[][] directions;
private int[][] visited;
public boolean existRecursive(char[][] board, String word) {
directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
visited = new int[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
boolean isExist = isExist(board, i, j, word, 0);
if (isExist) return true;
}
}
}
return false;
}
boolean isExist(char[][] board, int x, int y, String word, int index) {
if (index == word.length() - 1) return true;
visited[x][y] = 1;
for (int[] dir : directions) {
int x1 = x + dir[0];
int y1 = y + dir[1];
if (x1 >= 0 && x1 < board.length && y1 >= 0 && y1 < board[0].length
&& visited[x1][y1] == 0
&& word.charAt(index + 1) == board[x1][y1]) {
if (isExist(board, x1, y1, word, index + 1)) {
return true;
}
}
}
visited[x][y] = 0;
return false;
}
public boolean existIterative(char[][] board, String word) {
directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
boolean isExist = isExist(board, word, new int[]{i, j});
if (isExist) return true;
}
}
}
return false;
}
private boolean isExist(char[][] board, String word, int[] start) {
int[][] visited = new int[board.length][board[0].length];
int pointer = 0;
Deque<int[]> stack = new LinkedList<>();
stack.push(start);
while (!stack.isEmpty()) {
if (pointer == word.length()) {
return true;
}
int[] current = stack.pop();
if (visited[current[0]][current[1]] == 0) {
if (board[current[0]][current[1]] != word.charAt(pointer)) {
continue;
}
visited[current[0]][current[1]] = 1;
stack.push(current);
for (int[] dir : directions) {
int x = current[0] + dir[0];
int y = current[1] + dir[1];
if (x >= 0 && y >= 0 && x < board.length && y < board[0].length
&& visited[x][y] == 0) {
stack.push(new int[]{x, y});
}
}
pointer++;
} else if (visited[current[0]][current[1]] == 1) {
visited[current[0]][current[1]] = 0;
pointer--;
}
}
return false;
}
}