-
Notifications
You must be signed in to change notification settings - Fork 2
/
WordSearchII.java
113 lines (93 loc) · 3.14 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
108
109
110
111
112
113
package Leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* @author kalpak
*
* Given an m x n board of characters and a list of strings words, return all words on the board.
*
* Each word must be constructed from letters of sequentially adjacent cells,
* where adjacent cells are horizontally or vertically neighboring.
*
* The same letter cell may not be used more than once in a word.
*
* Example 1:
* Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
* Output: ["eat","oath"]
*
*
* Example 2:
* Input: board = [["a","b"],["c","d"]], words = ["abcb"]
* Output: []
*
*
* Constraints:
*
* m == board.length
* n == board[i].length
* 1 <= m, n <= 12
* board[i][j] is a lowercase English letter.
* 1 <= words.length <= 3 * 10^4
* 1 <= words[i].length <= 10
* words[i] consists of lowercase English letters.
* All the strings of words are unique.
*
*/
public class WordSearchII {
public static List<String> findWords(char[][] board, String[] words) {
List<String> result = new ArrayList<>();
TrieNode root = buildTrie(words);
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
searchWordDFS(board, result, root, i, j);
}
}
return result;
}
private static void searchWordDFS(char[][] board, List<String> result, TrieNode root, int i, int j) {
char c = board[i][j];
if(c == '#' || root.children[c - 'a'] == null)
return;
root = root.children[c - 'a'];
if(root.word != null) {
result.add(root.word);
root.word = null; // de-duplicate. No need of Set.
}
board[i][j] = '#'; // Track visisted cells
if(i > 0)
searchWordDFS(board, result, root, i - 1, j);
if(j > 0)
searchWordDFS(board, result, root, i, j - 1);
if(i < board.length - 1)
searchWordDFS(board, result, root, i + 1, j);
if(j < board[0].length - 1)
searchWordDFS(board, result, root, i, j + 1);
board[i][j] = c; // backtrack
}
private static TrieNode buildTrie(String[] words) {
TrieNode root = new TrieNode();
for(String word : words) {
TrieNode current = root;
for(char c : word.toCharArray()) {
if(current.children[c - 'a'] == null)
current.children[c - 'a'] = new TrieNode();
current = current.children[c - 'a'];
}
current.word = word;
}
return root;
}
static class TrieNode {
private TrieNode[] children;
private String word;
public TrieNode() {
this.children = new TrieNode[26];
this.word = null;
}
}
public static void main(String[] args) {
char[][] board = new char[][]{{'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}};
String[] words = new String[]{"oath", "pea", "eat", "rain"};
System.out.println(findWords(board, words));
}
}