题目:
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
Word Break I + DFS, 用动态规划实现Word Break I,先回答s能否被wordDict里的字符串表示的问题。然后用递归实现DFS,找出所有可能的组合。如果不加Word Break I而直接DFS会在某些测试用例上超时,如s="aaaaaaaaab", wordDict = ["a", "ba"]
c++版:
class Solution { public: vector<string> wordBreak(string s, unordered_set<string>& wordDict) { vector<string> result; if(s.length() == 0 || wordDict.size() == 0) return result; vector<bool> test(s.length()+1, false); test[0] = true; for(int i = 1; i <= s.length(); i++) { for(int j = 0; j < i; j++) { if(test[j] && wordDict.find(s.substr(j, i - j)) != wordDict.end()) { test[i] = true; break; } } } if(test[s.length()] == false) return result; if(wordDict.find(s) != wordDict.end()) result.push_back(s); for(int i = 1; i < s.length(); i++) { if(wordDict.find(s.substr(0, i)) != wordDict.end()) { string front = s.substr(0, i); vector<string> back = findRest(s.substr(i, s.length()-i), wordDict); if(back.size() != 0) { for(int j = 0; j < back.size(); j++) { result.push_back(front + " " + back[j]); } } } } return result; } vector<string> findRest(string s, unordered_set<string> &wordDict) { vector<string> result; if(wordDict.find(s) != wordDict.end()) result.push_back(s); if(s.length() == 1) return result; for(int i = 1; i < s.length(); i++) { if(wordDict.find(s.substr(0, i)) != wordDict.end()) { string front = s.substr(0, i); vector<string> back = findRest(s.substr(i, s.length()-i), wordDict); if(back.size() != 0) { for(int j = 0; j < back.size(); j++) { result.push_back(front + " " + back[j]); } } } } return result; } };
Java版:
public class Solution { public List<String> wordBreak(String s, Set<String> wordDict) { List<String> result = new ArrayList<>(); if(s == null || wordDict.isEmpty()) return result; int[] test = new int[s.length()+1]; test[0] = 1; for(int i = 1; i <= s.length(); i++) { for(int j = 0; j < i; j++) { if(test[j] == 1&& wordDict.contains(s.substring(j,i))) { test[i] = 1; } } } if(test[s.length()] == 0) return result; if(wordDict.contains(s)) { result.add(s); } for(int i = 1; i < s.length(); i++) { if(wordDict.contains(s.substring(0, i))) { String front = s.substring(0, i); List<String> back = checkRest(s.substring(i), wordDict); if(back.isEmpty() == false) { for (String back1 : back) { StringBuilder str = new StringBuilder(); str.append(front); str.append(" "); str.append(back1); result.add(str.toString()); } } } } return result; } public List<String> checkRest(String s, Set<String> wordDict) { List<String> result = new ArrayList<>(); if(s.length() == 1) { if(wordDict.contains(s)) { result.add(s); } return result; } if(wordDict.contains(s)) { result.add(s); } for(int i = 1; i < s.length(); i++) { if(wordDict.contains(s.substring(0,i))) { String front = s.substring(0, i); List<String> back = checkRest(s.substring(i), wordDict); if(back.isEmpty() == false) { for (String back1 : back) { StringBuilder str = new StringBuilder(); str.append(front); str.append(" "); str.append(back1); result.add(str.toString()); } } } } return result; } }
Python版:
class Solution: # @param s, a string # @param wordDict, a set<string> # @return a string[] def wordBreak(self, s, wordDict): result = [] if len(s) == 0 or len(wordDict) == 0: return result test = [False] * (len(s) + 1) test[0] = True for i in range(1, len(s)+1): for j in range(0, i): if test[j] and s[j:i] in wordDict: test[i] = True break; if test[len(s)] == False: return result if s in wordDict: result.append(s) for i in range(1, len(s)): front = s[0:i] if front in wordDict: back = self.findRest(s[i:len(s)], wordDict) if len(back) != 0: for j in back: result.append(front + " " + j) return result def findRest(self, s, wordDict): result = [] if s in wordDict: result.append(s) if len(s) == 1: return result for i in range(1, len(s)): front = s[0:i] if front in wordDict: back = self.findRest(s[i:len(s)], wordDict) if len(back) != 0: for j in back: result.append(front + " " + j) return result