# 425 Word Squares

w a l l
a r e a
l e a d
l a d y

(0,0) 这个点找到满足条件的,我们选择w。
(0,1) 我们要满足wa, a都可以作为开头的部分。
(0,2) 我们要满足wal, l都可以作为开头的部分。

(0, 0) prefix[root, root, root, root]  (root.w, root.w)  root[0] = w, root[0] = w
(0, 1) prefix[w, root, root, root]     (w.a, root.a)     root[0] = wa, root[1] = a
(0, 2) prefix[wa, a, root, root]     (wa.l, root.l)     root[0] = wal, root[2] = l
(0, 3) prefix[wal, a, l, root]     (wal.l, root.l)     root[0] = wall, root[3] = l

(1,1)  prefix[wall, a, l, l]       (a.r, a.r)     root[1] = ar,  root[1] = ar
(1,2)  prefix[wall, ar, l, l]      (ar.e, l.e)    root[1] = are, root[2] = le
(1,3)  prefix[wall, are, le, l]      (are.a, l.a)    root[1] = area, root[3] =la

public class Solution {
class Node{
String word = null;
Node[] kids = new Node[26];
}

private Node root = new Node();

private void buildTrie(String word, Node par){
for(char c: word.toCharArray()){
int idx = c -'a';
if(par.kids[idx] == null) par.kids[idx] = new Node();
par = par.kids[idx];
}
par.word = word;
}

private void findAllSquares(int row , int col, Node[] prefix, List<List<String>> res){
if(row == prefix.length){
List<String> temp = new ArrayList<String>();
for(int i=0; i<prefix.length; i++){
}
} else if(col < prefix.length){
Node currow = prefix[row];
Node curcol = prefix[col];
for(int i=0; i<26; i++){
if(currow.kids[i] != null && curcol.kids[i] != null){   // 同一层从左向右走的时候
prefix[row] =  currow.kids[i];      //  prefix[row]会不断增长，直到最后形成以个单词
prefix[col] =  curcol.kids[i];      //  prefix[col]相应位置长度加一，更新prefix
findAllSquares(row, col+1, prefix, res);
}
}
prefix[row] = currow;       // reset back  重新进行下一次的搜索。
prefix[col] = curcol;
} else {
findAllSquares(row+1, row+1, prefix, res);
}
}

public List<List<String>> wordSquares(String[] words) {
List<List<String>> res = new ArrayList<List<String>>();
if(words == null || words.length == 0 || words[0] == null || words[0].length() == 0) return res;
for(String word: words){
buildTrie(word, root);
}
Node[] prefix = new Node[words[0].length()];
Arrays.fill(prefix, root);
findAllSquares(0, 0, prefix, res);
return res;
}
}

12 声望
4 粉丝
0 条评论