-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathWordSearchII.java
104 lines (90 loc) · 3.07 KB
/
WordSearchII.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
99
100
101
102
103
104
// Solution 1
class Solution {
public List<String> findWords(char[][] board, String[] words) {
Set<String> hashSet = new HashSet<>();
int n = board.length;
int m = board[0].length;
if(n==0 || m ==0){
return new ArrayList<>();
}
for(String word: words){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
boolean[][] visited = new boolean[n][m];
if(word.charAt(0) == board[i][j]){
if(dfs(word, 0, i, j, visited, board)){
hashSet.add(word);
}
}
}
}
}
return new ArrayList<>(hashSet);
}
private boolean dfs(String word, int len, int i, int j, boolean[][] visited, char[][] board){
if(len==word.length()){
return true;
}
if(i<0 || j<0 || i>=board.length || j>=board[0].length || word.charAt(len)!=board[i][j] || visited[i][j]){
return false;
}
boolean res = false;
visited[i][j] = true;
res = dfs(word, len+1, i-1, j, visited, board) ||
dfs(word, len+1, i+1, j, visited, board) ||
dfs(word, len+1, i, j+1, visited, board) ||
dfs(word, len+1, i, j-1, visited, board);
visited[i][j] = false;
return res;
}
}
// Solution II
class Solution {
private class TrieNode {
private TrieNode[] children;
private String word;
TrieNode(){
children = new TrieNode[26];
}
}
public List<String> findWords(char[][] board, String[] words) {
List<String> res = new ArrayList<>();
TrieNode root = buildTrie(words);
int m= board.length, n = board[0].length;
// System.out.println(" Value is " + root.children['o' -'c']);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
dfs(board, i, j, root, res);
}
}
return res;
}
public void dfs(char[][] board, int i, int j, TrieNode p, List<String> res) {
char c = board[i][j];
if (c == ';' || p.children[c - 'a'] == null) return;
p = p.children[c - 'a'];
if (p.word != null) { // found one
res.add(p.word);
p.word = null; // de-duplicate
}
board[i][j] = ';';
if (i > 0) dfs(board, i - 1, j ,p, res);
if (j > 0) dfs(board, i, j - 1, p, res);
if (i < board.length - 1) dfs(board, i + 1, j, p, res);
if (j < board[0].length - 1) dfs(board, i, j + 1, p, res);
board[i][j] = c;
}
public TrieNode buildTrie(String[] words) {
TrieNode root = new TrieNode();
for (String w : words) {
TrieNode p = root;
for (char c : w.toCharArray()) {
int i = c - 'a';
if (p.children[i] == null) p.children[i] = new TrieNode();
p = p.children[i];
}
p.word = w;
}
return root;
}
}