-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution212.java
98 lines (81 loc) · 2.5 KB
/
Solution212.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
package algorithm.leetcode;
import java.util.*;
/**
* @author: mayuan
* @desc: 单词搜索 II
* @date: 2018/08/12
*/
public class Solution212 {
public static void main(String[] args) {
char[][] board = new char[][]{{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'}};
char[][] board2 = new char[][]{{'a', 'a'}};
String[] words = new String[]{"oath", "pea", "eat", "rain"};
String[] words2 = new String[]{"a"};
Solution212 test = new Solution212();
List<String> ans = test.findWords(board, words);
ans.forEach(System.out::println);
}
public List<String> findWords(char[][] board, String[] words) {
List<String> result = new ArrayList<>(words.length);
// 构建前缀树
TrieNode root = buildTrie(words);
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
dfs(board, root, result, i, j);
}
}
Collections.sort(result);
return result;
}
private void dfs(char[][] board, TrieNode p, List<String> answer, int x, int y) {
char c = board[x][y];
if ('#' == c || null == p.children[c - 'a']) {
return;
}
p = p.children[c - 'a'];
if (null != p.word) {
if (!answer.contains(p.word)) {
answer.add(p.word);
}
}
board[x][y] = '#';
if (x > 0) {
dfs(board, p, answer, x - 1, y);
}
if (y > 0) {
dfs(board, p, answer, x, y - 1);
}
if (x < board.length - 1) {
dfs(board, p, answer, x + 1, y);
}
if (y < board[x].length - 1) {
dfs(board, p, answer, x, y + 1);
}
board[x][y] = c;
}
private TrieNode buildTrie(String[] words) {
TrieNode root = new TrieNode();
for (String word : words) {
TrieNode cur = root;
for (char letter : word.toCharArray()) {
int index = letter - 'a';
if (null == cur.children[index]) {
cur.children[index] = new TrieNode();
}
cur = cur.children[index];
}
cur.word = word;
}
return root;
}
class TrieNode {
TrieNode[] children;
String word;
public TrieNode() {
children = new TrieNode[26];
}
}
}