-
Notifications
You must be signed in to change notification settings - Fork 251
/
BoggleGame.java
192 lines (163 loc) · 6 KB
/
BoggleGame.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package boggle_game;
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;
public class BoggleGame {
/*
Boggle Game
AirBnB Interview Question
*/
public class Solution {
private final int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
private String path2Word(char[][] board, List<int[]> curPath) {
StringBuilder sb = new StringBuilder();
for (int[] coor : curPath) {
sb.append(board[coor[0]][coor[1]]);
}
return sb.toString();
}
private void search(List<List<int[]>> paths, char[][] board, int x, int y, Trie trie,
boolean[][] visited, List<int[]> curPath) {
String curWord = path2Word(board, curPath);
ReturnType flag = trie.search(curWord);
if (!flag.hasPrefix) {
return;
}
if (flag.hasWord) {
paths.add(new ArrayList<>(curPath));
}
int m = board.length;
int n = board[0].length;
for (int[] dir : dirs) {
int xx = x + dir[0];
int yy = y + dir[1];
if (xx < 0 || xx >= m || yy < 0 || yy >= n) {
continue;
}
visited[xx][yy] = true;
curPath.add(new int[]{xx, yy});
search(paths, board, xx, yy, trie, visited, curPath);
curPath.remove(curPath.size() - 1);
visited[xx][yy] = false;
}
}
private void searchWords(List<String> res, List<String> curWords, List<List<int[]>> paths,
int start, boolean[][] visited, char[][] board) {
if (start == paths.size()) {
if (curWords.size() > res.size()) {
res.clear();
res.addAll(curWords);
}
return;
}
for (int i = start; i < paths.size(); i++) {
boolean canUse = true;
for (int[] coor : paths.get(i)) {
if (visited[coor[0]][coor[1]]) {
canUse = false;
break;
}
}
if (canUse) {
for (int[] coor : paths.get(i)) {
visited[coor[0]][coor[1]] = true;
}
curWords.add(path2Word(board, paths.get(i)));
searchWords(res, curWords, paths, i + 1, visited, board);
curWords.remove(curWords.size() - 1);
for (int[] coor : paths.get(i)) {
visited[coor[0]][coor[1]] = false;
}
}
}
}
public List<String> findMostStr(char[][] board, Set<String> dict) {
List<List<int[]>> paths = new ArrayList<>();
Trie trie = new Trie();
for (String word : dict) {
trie.insert(word);
}
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
boolean[][] visited = new boolean[m][n];
visited[i][j] = true;
List<int[]> curPath = new ArrayList<>();
curPath.add(new int[]{i, j});
search(paths, board, i, j, trie, visited, curPath);
}
}
List<String> res = new ArrayList<>();
searchWords(res, new ArrayList<>(), paths, 0, new boolean[m][n], board);
return res;
}
class ReturnType {
boolean hasPrefix;
boolean hasWord;
ReturnType(boolean hasPrefix, boolean hasWord) {
this.hasPrefix = hasPrefix;
this.hasWord = hasWord;
}
}
class TrieNode {
char c;
boolean isEnd;
Map<Character, TrieNode> children;
public TrieNode(char c, boolean isEnd) {
this.c = c;
this.isEnd = isEnd;
this.children = new HashMap<>();
}
}
class Trie {
private TrieNode root;
public Trie() {
this.root = new TrieNode(' ', false);
}
public void insert(String word) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!cur.children.containsKey(c)) {
cur.children.put(c, new TrieNode(c, false));
}
cur = cur.children.get(c);
}
cur.isEnd = true;
}
public ReturnType search(String word) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!cur.children.containsKey(c)) {
return new ReturnType(false, false);
}
cur = cur.children.get(c);
}
return new ReturnType(true, cur.isEnd);
}
}
}
public static class UnitTest {
@Test
public void test1() {
Solution sol = new BoggleGame().new Solution();
char[][] board = {
{'o', 'a', 't', 'h'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'}
};
Set<String> dict = new HashSet<>();
dict.add("oath");
dict.add("pea");
dict.add("eat");
dict.add("rain");
List<String> res = sol.findMostStr(board, dict);
assertEquals(2, res.size());
assertEquals("oath", res.get(0));
assertEquals("eat", res.get(1));
}
}
}