forked from ByteByteGoHq/coding-interview-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAllWordsOnABoard.java
More file actions
69 lines (65 loc) · 2.43 KB
/
FindAllWordsOnABoard.java
File metadata and controls
69 lines (65 loc) · 2.43 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class TrieNode {
Map<Character, TrieNode> children;
String word;
public TrieNode() {
this.children = new HashMap<>();
this.word = null;
}
}
public class FindAllWordsOnABoard {
public List<String> findAllWordsOnABoard(char[][] board, String[] words) {
TrieNode root = new TrieNode();
// Insert every word into the trie.
for (String word : words) {
TrieNode node = root;
for (char c : word.toCharArray()) {
node.children.putIfAbsent(c, new TrieNode());
node = node.children.get(c);
}
node.word = word;
}
List<String> res = new ArrayList<>();
// Start a DFS call from each cell of the board that contains a
// child of the root node, which represents the first letter of a
// word in the trie.
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[0].length; c++) {
if (root.children.containsKey(board[r][c])) {
dfs(board, r, c, root.children.get(board[r][c]), res);
}
}
}
return res;
}
private void dfs(char[][] board, int r, int c, TrieNode node, List<String> res) {
// If the current node represents the end of a word, add the word to
// the result.
if (node.word != null) {
res.add(node.word);
// Ensure the current word is only added once.
node.word = null;
}
char temp = board[r][c];
// Mark the current cell as visited.
board[r][c] = '#';
// Explore all adjacent cells that correspond with a child of the
// current TrieNode.
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int[] d : dirs) {
int nextR = r + d[0];
int nextC = c + d[1];
if (isWithinBounds(nextR, nextC, board) && node.children.containsKey(board[nextR][nextC])) {
dfs(board, nextR, nextC, node.children.get(board[nextR][nextC]), res);
}
}
// Backtrack by reverting the cell back to its original character.
board[r][c] = temp;
}
private boolean isWithinBounds(int r, int c, char[][] board) {
return 0 <= r && r < board.length && 0 <= c && c < board[0].length;
}
}