-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path140.word-break-ii.java
33 lines (25 loc) · 953 Bytes
/
140.word-break-ii.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
class Solution {
public List<String> wordBreak(String s, List<String> wordDict) {
if (s.length() > 100) return new ArrayList();
List<String> res = new ArrayList<String>();
wordHelper(s, wordDict, res, new StringBuilder());
return res;
}
public void wordHelper(String s, List<String> words, List<String> res, StringBuilder str){
if(str.length() != 0){
str.append(" ");
}
for(String word: words){
StringBuilder curr = new StringBuilder(str);
if(s.startsWith(word)){
curr.append(word);
if(s.length() == word.length()){
res.add(curr.toString());
}
else{
wordHelper(s.substring(word.length()), words, res, curr);
}
}
}
}
}