LeetCode Top Interview Questions 140. Word Break II (Java版; Hard)
题目描述
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Example 1:
Input:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
Output:
[
"cats and dog",
"cat sand dog"
]
Example 2:
Input:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
Output:
[
"pine apple pen apple",
"pineapple pen apple",
"pine applepen apple"
]
Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
Output:
[]
classSolution{public List<String>wordBreak(String s, List<String> wordDict){
List<String> list =newArrayList<>();
StringBuilder sb =newStringBuilder();int n = s.length();if(n==0|| wordDict.size()==0){return list;}
HashMap<String, List<String>> map =newHashMap<>();
list =dfs(wordDict, s, map);return list;}private List<String>dfs(List<String> wordDict, String s, HashMap<String, List<String>> map){//递归出口if(map.containsKey(s)){return map.get(s);}
List<String> res =newArrayList<>();int n = s.length();//递归出口if(n==0){
res.add("");return res;}for(String word : wordDict){if(s.startsWith(word)){
List<String> tmp =dfs(wordDict, s.substring(word.length()), map);if(tmp.size()!=0){for(String t : tmp){if(t.equals("")){
res.add(word);}else{
res.add(word+" "+t);}}}}}
map.put(s, res);return res;}}
/*
暴力递归; 回溯; 看看s[index,end]是不是有效单词, 是的话执行新条件新递归; 不是的话继续下一轮循环
回溯就是DFS的体现
报错:出现了重复
输入
"aaaaaaa"
["aaaa","aa","a"]
输出
["a a a a a a a","a a a a a aa","a a a a aa a","a a a aa a a",
"a aa a a aa","a a a aaaa","a a aa a a a","aa a a a aa","a a aa aa a",
"a a aaaa a","a aa a a a a","aa a a a aa","a a aa aa a","aa a aa a a",
"aa aa a aa","a aa aaaa","a aaaa a a","aaaa a aa","aa a a a a a",
"aa a a a aa","a a aa aa a","aa a aa a a","aa aa a aa","a aa aaaa",
"aa aa a a a","aa aa a aa","aa aa aa a","aa aaaa a","aaaa a a a",
"aaaa a aa","aaaa aa a"]
*/classSolution{public List<String>wordBreak(String s, List<String> wordDict){
List<String> res =newArrayList<>();
List<String> al =newArrayList<>();backtrace(res, al, wordDict, s,0);return res;}publicvoidbacktrace(List<String> res, List<String> al, List<String> wordDict, String s,int index){//base caseif(index==s.length()){
StringBuilder sb =newStringBuilder();for(String ss : al)
sb.append(ss).append(" ");
res.add(sb.toString().trim());return;}//从index位置开始处理sfor(int i=index; i<s.length(); i++){//取出s[index,i], 要时刻明确递归函数处理的是哪部分
String cur = s.substring(index, i+1);if(wordDict.contains(cur)){//改变现场
al.add(cur);//新条件新递归; 从i+1位置开始处理backtrace(res, al, wordDict, s, i+1);//恢复现场; 核心:要删除最后一个, 也就是说得用索引指定待删除元素的位置// al.remove(cur);
al.remove(al.size()-1);}}}}
LeetCode Top Interview Questions 140. Word Break II (Java版; Hard)
welcome to my blogLeetCode Top Interview Questions 140. Word Break II (Java版; Hard)题目描述Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to ...