Skip to content

Commit 3a529fd

Browse files
140. Word Break II (java)
1 parent 6c91538 commit 3a529fd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public List<String> wordBreak(String s, List<String> wordDict) {
3+
return wordBreak(s,wordDict,new HashMap<>(16));
4+
}
5+
private List<String> wordBreak(String s, List<String> wordDict, HashMap<String, List<String>> map) {
6+
List<String> list=new ArrayList<>();
7+
if(map.containsKey(s)) return map.get(s);
8+
if("".equals(s)){
9+
list.add("");
10+
return list;
11+
}
12+
for(String word:wordDict){
13+
if(s.startsWith(word)){
14+
List<String> res=wordBreak(s.substring(word.length()),wordDict,map);
15+
for(String str:res){
16+
list.add(word+("".equals(str) ?"":" ")+str);
17+
}
18+
}
19+
}
20+
map.put(s,list);
21+
return list;
22+
}
23+
}

0 commit comments

Comments
 (0)