-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordBreak2.java
58 lines (47 loc) · 2.22 KB
/
WordBreak2.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
/*
* Key point: dp strategy, but no explicit dp status array
* when build solution, also use recursive.
*
* The idea is: create a list for each characters, then from right to left, if
* a character is the start of a word, store the word's ending character's
* index(actually it's plus one, for the sake of substring()) to its list.
* After one pass, we can base the record to build all possible solutions.
*/
public class Solution {
ArrayList<String> wordBreak(String s, Set<String> dict) {
int length = s.length();
//create the word ending character's index list for each character
ArrayList<ArrayList<Integer>> record = new ArrayList<ArrayList<Integer>>(length);
for(int i = 0;i<length;i++)
record.add(new ArrayList<Integer>());
//each character can be the ending character of some word
for(int end=length;end>=0;end--){
if(end < length && record.get(end).isEmpty())
continue;
//find the starting character for the current ending character
for(int runner = end-1;runner >= 0;runner--){
if(dict.contains(s.substring(runner,end)))
record.get(runner).add(end); //add current end to start character's list
}
}
ArrayList<String> solutionSet = new ArrayList<String>();
buildSolution(record,0,s,"",solutionSet);
return solutionSet;
}
void buildSolution(ArrayList<ArrayList<Integer>> record, int current, String s,
String solution, ArrayList<String> solutionSet){
//iterate on current character's word ending list
for(Integer end : record.get(current)){
String sub=s.substring(current,end);
/*
since the loop may have many iterations, we should keep the reference
of "solution", rather than writing as "solution += ..."
*/
String newSolution = solution+(current==0 ? sub : " "+sub);
if(end == s.length())
solutionSet.add(newSolution);
else
buildSolution(record,end,s,newSolution,solutionSet);
}
}
}