-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordSearchII.java
107 lines (92 loc) · 3.44 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
105
106
107
package solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// [Problem] https://leetcode.com/problems/word-search-ii
class WordTrieNode {
String word;
Map<Character, WordTrieNode> children;
public WordTrieNode() {
word = null;
children = new HashMap<>();
}
}
class WordSearchII {
private static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // up, down, left, right
private char[][] board;
private int rowSize;
private int colSize;
// Backtracking with Trie
public List<String> findWords(char[][] board, String[] words) {
this.board = board;
this.rowSize = board.length;
this.colSize = board[0].length;
WordTrieNode root = buildTrie(words);
List<String> found = new ArrayList<>();
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < colSize; col++) {
if (root.children.containsKey(board[row][col])) {
backtrack(row, col, root, found);
}
}
}
return found;
}
private WordTrieNode buildTrie(String[] words) {
WordTrieNode root = new WordTrieNode();
for (String word : words) {
WordTrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
if (!node.children.containsKey(letter)) {
node.children.put(letter, new WordTrieNode());
}
node = node.children.get(letter);
}
node.word = word;
}
return root;
}
private void backtrack(int row, int col, WordTrieNode node, List<String> foundWords) {
char letter = board[row][col];
WordTrieNode currentNode = node.children.get(letter);
if (currentNode.word != null) {
foundWords.add(currentNode.word);
currentNode.word = null;
}
board[row][col] = '#';
for (int[] direction : DIRECTIONS) {
int nextRow = row + direction[0];
int nextCol = col + direction[1];
if (nextRow >= 0 && nextRow < rowSize && nextCol >= 0 && nextCol < colSize
&& currentNode.children.containsKey(board[nextRow][nextCol])) {
backtrack(nextRow, nextCol, currentNode, foundWords);
}
}
board[row][col] = letter;
if (currentNode.children.isEmpty()) {
node.children.remove(letter);
}
}
// Test
public static void main(String[] args) {
WordSearchII solution = new WordSearchII();
char[][] board1 = {
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'}};
String[] words1 = {"oath", "pea", "eat", "rain"};
List<String> expectedOutput1 = List.of("oath", "eat");
List<String> actualOutput1 = solution.findWords(board1, words1);
System.out.println("Test 1 passed? " + expectedOutput1.equals(actualOutput1));
char[][] board2 = {
{'a', 'b'},
{'c', 'd'}};
String[] words2 = {"abcb"};
List<String> expectedOutput2 = new ArrayList<>();
List<String> actualOutput2 = solution.findWords(board2, words2);
System.out.println("Test 2 passed? " + expectedOutput2.equals(actualOutput2));
}
}