forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordSquares.java
73 lines (43 loc) · 2 KB
/
wordSquares.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
// 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.
public class Solution {
public List<List<String>> wordSquares(String[] words) {
List<List<String>> ret = new ArrayList<List<String>>();
if(words.length==0 || words[0].length()==0) return ret;
Map<String, Set<String>> map = new HashMap<>();
int squareLen = words[0].length();
// create all prefix
for(int i=0;i<words.length;i++){
for(int j=-1;j<words[0].length();j++){
if(!map.containsKey(words[i].substring(0, j+1))) map.put(words[i].substring(0, j+1), new HashSet<String>());
map.get(words[i].substring(0, j+1)).add(words[i]);
}
}
helper(ret, new ArrayList<String>(), 0, squareLen, map);
return ret;
}
public void helper(List<List<String>> ret, List<String> cur, int matched, int total, Map<String, Set<String>> map){
if(matched == total) {ret.add(new ArrayList<String>(cur));return;}
// build search string
StringBuilder sb = new StringBuilder();
for(int i=0;i<=matched-1;i++) sb.append(cur.get(i).charAt(matched));
// bachtracking
Set<String> cand = map.get(sb.toString());
if(cand==null) return;
for(String str:cand){
cur.add(str);
helper(ret, cur, matched+1, total, map);
cur.remove(cur.size()-1);
}
}
}