forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path212_findWords.java
60 lines (54 loc) · 1.82 KB
/
212_findWords.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
public class Solution {
public List<String> findWords(char[][] board, String[] words) {
TrieNode root = buildTrieNode(words);
List<String> res = new ArrayList<String>();
int m = board.length;
int n = board[0].length;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
dfs(board, root, i, j, res);
}
}
return res;
}
public void dfs(char[][] board, TrieNode node, int i, int j, List<String> res){
int m = board.length;
int n = board[0].length;
char c = board[i][j];
if(c == '*' || node.children[c-'a'] == null) return;
node = node.children[c-'a'];
if(node.word!= null){ //find one
res.add(node.word);
node.word = null; // de-duplicate!!!!
}
board[i][j] = '*';
if (i > 0) dfs(board,node, i - 1, j , res); // write here can get rid of lots of trouble later.
if (j > 0) dfs(board, node, i, j - 1, res);
if (i < m-1) dfs(board, node, i + 1, j, res);
if (j < n-1) dfs(board, node, i, j + 1,res);
board[i][j] = c;//backtracking!!
}
public TrieNode buildTrieNode(String[] words){
TrieNode root = new TrieNode();
for(String word: words){
TrieNode run = root;
int i = 0;
while(i < word.length()){
char c = word.charAt(i);
if(run.children[c-'a'] == null) run.children[c-'a'] = new TrieNode();
run = run.children[c-'a'];
i++;
}
run.word = word;
}
return root;
}
public class TrieNode{
String word;
TrieNode[] children;
public TrieNode(){
this.word = null;
this.children = new TrieNode[26];
}
}
}