-
Notifications
You must be signed in to change notification settings - Fork 35
/
73--word-search-ii.java
58 lines (51 loc) · 1.64 KB
/
73--word-search-ii.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
class Solution {
class Node {
HashMap<Character, Node> child;
String word;
public Node() {
child = new HashMap<>();
}
}
public List<String> findWords(char[][] board, String[] words) {
List<String> list = new ArrayList<>();
Node root = buildTrie(words);
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
dfs(board, list, root, i, j);
}
}
return list;
}
public void dfs(char[][] board, List<String> list, Node curr, int i, int j) {
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length) return;
char ch = board[i][j];
if (ch == '#' || curr.child.get(ch) == null) return;
curr = curr.child.get(ch);
if (curr.word != null) {
list.add(curr.word);
curr.word = null;
}
board[i][j] = '#';
// top, left, down, right
dfs(board, list, curr, i - 1, j);
dfs(board, list, curr, i, j - 1);
dfs(board, list, curr, i + 1, j);
dfs(board, list, curr, i, j + 1);
board[i][j] = ch;
}
public Node buildTrie(String[] words) {
Node root = new Node();
for (String word : words) {
Node curr = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (!curr.child.containsKey(ch)) {
curr.child.put(ch, new Node());
}
curr = curr.child.get(ch);
}
curr.word = word;
}
return root;
}
}