-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
76 lines (64 loc) · 2.28 KB
/
Copy pathSolution.java
File metadata and controls
76 lines (64 loc) · 2.28 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
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Solution {
static class TrieNode {
TrieNode[] children = new TrieNode[26];
boolean end;
static void addToTrie(String word, TrieNode parent) {
TrieNode current = parent;
for (char ch : word.toCharArray()) {
int idx = ch - 'a';
if (current.children[idx] == null) {
current.children[idx] = new TrieNode();
}
current = current.children[idx];
}
current.end = true;
}
}
public List<String> findWords(char[][] board, String[] wordsArr) {
TrieNode parent = new TrieNode();
for (String word : wordsArr) {
TrieNode.addToTrie(word, parent);
}
int n = board.length, m = board[0].length;
boolean[][] visited = new boolean[n][m];
Set<String> result = new HashSet<>();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < m; j++) {
backtrack(i, j, "", board, visited, result, parent);
}
}
return new ArrayList<>(result);
}
private void backtrack(int i, int j, String current, char[][] board,
boolean[][] visited, Set<String> result, TrieNode currentNode) {
if (currentNode.end) {
result.add(current);
}
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length) {
return;
}
if (visited[i][j]) {
return;
}
char ch = board[i][j];
TrieNode next = currentNode.children[ch - 'a'];
if (next == null) {
return;
}
// Set the next arguments.
visited[i][j] = true;
StringBuilder sb = new StringBuilder(current);
sb.append(ch);
backtrack(i - 1, j, sb.toString(), board, visited, result, next);
backtrack(i + 1, j, sb.toString(), board, visited, result, next);
backtrack(i, j - 1, sb.toString(), board, visited, result, next);
backtrack(i, j + 1, sb.toString(), board, visited, result, next);
// Reset arguments.
visited[i][j] = false;
sb.deleteCharAt(sb.length() - 1);
}
}