-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWordSearch2.java
More file actions
63 lines (57 loc) · 1.87 KB
/
WordSearch2.java
File metadata and controls
63 lines (57 loc) · 1.87 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
public class Solution {
public List<String> findWords(char[][] board, String[] words) {
Trie trie = new Trie();
for (String word : words) trie.insert(word);
List<String> res = new ArrayList<>();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
dfs(board, i, j, trie.root, "", res);
}
}
return res;
}
public void dfs(char[][] board, int i, int j, TrieNode node, String word, List<String> res) {
if (node == null || i < 0 || i >= board.length
|| j < 0 || j >= board[0].length
|| board[i][j] == '#') return;
char c = board[i][j];
int index = c - 'a';
if (node.list[index] != null && node.list[index].leaf) {
res.add(word + c);
node.list[index].leaf = false;
}
board[i][j] = '#';
dfs(board, i - 1, j, node.list[index], word + c, res);
dfs(board, i + 1, j, node.list[index], word + c, res);
dfs(board, i, j - 1, node.list[index], word + c, res);
dfs(board, i, j + 1, node.list[index], word + c, res);
board[i][j] = c;
}
}
class TrieNode {
// Initialize your data structure here.
public TrieNode[] list;
public boolean leaf;
public TrieNode() {
list = new TrieNode[26];
leaf = false;
}
}
class Trie {
public TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (current.list[index] == null) {
current.list[index] = new TrieNode();
}
current = current.list[index];
}
current.leaf = true;
}
}