-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathWordSquares425.java
198 lines (178 loc) · 5.39 KB
/
WordSquares425.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Given a set of words (without duplicates), find all word squares you can
* build from them.
*
* A sequence of words forms a valid word square if the kth row and column read
* the exact same string, where 0 ≤ k < max(numRows, numColumns).
*
* For example, the word sequence ["ball","area","lead","lady"] forms a word
* square because each word reads the same both horizontally and vertically.
*
* b a l l
* a r e a
* l e a d
* l a d y
*
* Note:
* - There are at least 1 and at most 1000 words.
* - All words will have the exact same length.
* - Word length is at least 1 and at most 5.
* - Each word contains only lowercase English alphabet a-z.
*
* Example 1:
*
* Input:
* ["area","lead","wall","lady","ball"]
*
* Output:
* [
* [ "wall",
* "area",
* "lead",
* "lady"
* ],
* [ "ball",
* "area",
* "lead",
* "lady"
* ]
* ]
*
* Explanation:
* The output consists of two word squares. The order of output does not matter
* (just the order of words in each word square matters).
*
* Example 2:
*
* Input:
* ["abat","baba","atan","atal"]
*
* Output:
* [
* [ "baba",
* "abat",
* "baba",
* "atan"
* ],
* [ "baba",
* "abat",
* "baba",
* "atal"
* ]
* ]
*
* Explanation:
* The output consists of two word squares. The order of output does not matter
* (just the order of words in each word square matters).
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class WordSquares425 {
public List<List<String>> wordSquares(String[] words) {
List<List<String>> res = new ArrayList<>();
if (words == null || words.length == 0 || words[0].length() == 0) return res;
int len = words[0].length();
TrieNode root = new TrieNode();
for (String word: words) root.addWord(word);
helper(new char[len][len], root, 0, len, new ArrayList<String>(), res);
return res;
}
private void helper(char[][] square, TrieNode root, int pos, int len, List<String> sqr, List<List<String>> res) {
if (pos == len) {
if (sqr.size() == len) {
List<String> newSqr = new ArrayList<String>();
for (String s: sqr) {
newSqr.add(s);
}
res.add(newSqr);
}
return;
}
List<String> finds = root.search(Arrays.copyOfRange(square[pos], 0, pos), len);
if (finds.size() == 0) return;
for (String w: finds) {
sqr.add(w);
for (int j=pos; j<len; j++) square[j][pos] = w.charAt(j);
helper(square, root, pos+1, len, sqr, res);
sqr.remove(sqr.size() - 1);
}
}
class TrieNode {
boolean isWord = false;
String word = null;
TrieNode[] children = new TrieNode[26];
public void addWord(String word) {
addWord(word.toCharArray(), 0);
}
public void addWord(char[] chars, int i) {
if (i == chars.length) {
this.isWord = true;
this.word = new String(chars);
return;
}
int idx = chars[i] - 'a';
if (children[idx] == null) {
TrieNode t = new TrieNode();
children[idx] = t;
}
children[idx].addWord(chars, i+1);
}
public List<String> search(char[] prefix, int length) {
List<String> res = new ArrayList<String>();
search(prefix, length, 0, res);
return res;
}
public void search(char[] prefix, int length, int i, List<String> res) {
if (i == length) {
if (word != null) res.add(word);
return;
}
if (i < prefix.length) {
if (children[prefix[i] - 'a'] == null) return;
children[prefix[i] - 'a'].search(prefix, length, i+1, res);
} else {
for (TrieNode child: children) {
if (child != null) child.search(prefix, length, i+1, res);
}
}
}
}
public List<List<String>> wordSquares2(String[] words) {
Arrays.sort(words);
List<List<String>> res = new ArrayList<>();
int W = words[0].length();
char[][] board = new char[W][W];
helper(board, 0, words, res, W);
return res;
}
private void helper(char[][] board, int i, String[] words, List<List<String>> res, int W) {
if (i >= W) {
List<String> r = new ArrayList<>();
for (int j=0; j<W; j++) {
r.add(new String(board[j]));
}
res.add(r);
return;
}
for (int j=i; j<W; j++) {
board[i][j] = 'a';
}
String start = new String(board[i]);
String prefix = new String(board[i], 0, i);
int idx = Arrays.binarySearch(words, start);
if (idx < 0) idx = - (idx + 1);
for (int k=idx; k<words.length; k++) {
String key = words[k];
if (!key.startsWith(prefix)) break;
char[] chars = key.toCharArray();
for (int j=0; j<W; j++) {
board[i][j] = chars[j];
}
for (int j=0; j<W; j++) {
board[j][i] = chars[j];
}
helper(board, i+1, words, res, W);
}
}
}