-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay-30 Word Search II
71 lines (56 loc) · 1.69 KB
/
Day-30 Word Search II
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
class Solution {
List<String> result;
char[][] board;
public List<String> findWords(char[][] board, String[] words) {
result=new ArrayList<>();
this.board=board;
Trie root=new Trie();
for(String s:words){
Trie temp=root;
for(char c:s.toCharArray()){
if(temp.t[c-'a']==null){
temp.t[c-'a']=new Trie();
}
temp=temp.t[c-'a'];
}
temp.end=true;
temp.word=s;
}
int m=board.length;
int n=board[0].length;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(root.t[board[i][j]-'a']!=null){
helper(i,j,root);
}
}
}
return result;
}
void helper(int row,int col,Trie root){
if(row<0 || col<0 || row>= board.length || col >= board[0].length || board[row][col]=='#' || root.t[board[row][col]-'a']==null){
return;
}
if(root.t[board[row][col]-'a'].end){
result.add(root.t[board[row][col]-'a'].word);
root.t[board[row][col]-'a'].end=false;
}
root=root.t[board[row][col]-'a'];
char c=board[row][col];
board[row][col]='#';
helper(row+1,col,root);
helper(row-1,col,root);
helper(row,col+1,root);
helper(row,col-1,root);
board[row][col]=c;
}
class Trie{
boolean end;
Trie[] t;
String word;
Trie(){
end=false;
t=new Trie[26];
}
}
}