单词拆分I
题目:
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
说明:
拆分时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:
输入: s = “leetcode”, wordDict = [“leet”, “code”]
输出: true
解释: 返回 true 因为 “leetcode” 可以被拆分成 “leet code”。
示例 2:
输入: s = “applepenapple”, wordDict = [“apple”, “pen”]
输出: true
解释: 返回true 因为 “applepenapple” 可以被拆分成 “apple pen apple”。注意你可以重复使用字典中的单词。
示例 3:
输入: s = “catsandog”, wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
输出: false
- 思路一(超时)
比较容易想到的解法是,从字符串 s 开头处开始匹配,若匹配到字典里的字符串,则继续匹配剩下的字符串。这样递归下去,直到找到一个可行的序列或尝试所有的单词组合。
代码:
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if(s.length() == 0) {
return true;
}
else {
for(int i = 1; i <= s.length(); i++) {
string subs = s.substr(0, i);
if(find(wordDict.begin(), wordDict.end(), subs) != wordDict.end()) {
if(wordBreak(s.substr(i,s.length()), wordDict)) {
return true;
}
}
}
return false;
}
}
};
输出超时:
- 思路二
上述思路一的做法会造成大量冗余,我们其实并不关心如何用几种方式进行切分,只是关心能否切分。
新的思路是:新建一个数组vector isbreak(n, false),每个位置记录下该位置之前的子串是否可以被分割,这样只需要记录每个位置之前的子串能否被切分就好了。这样我们判断当前位置i之前能否被切分时,只需要判断从i之前所有可以被切分的位置j到i的子串能否在字典中找到即可,即:
if(isbreak[j] && find(wordDict.begin(), wordDict.end(), s.substr(j,i-j)) != wordDict.end()) {
isbreak[i] = true;
}
代码:
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int n = s.length();
vector<bool> isbreak(n, false);
isbreak[0] = true;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= i; j++) {
if(isbreak[j] && find(wordDict.begin(), wordDict.end(), s.substr(j,i-j)) != wordDict.end()) {
isbreak[i] = true;
}
}
}
return isbreak[n];
}
};
——————————————————————————————————
单词拆分 II
进一步的,要求把所有拆分结果都写出来。
题目
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。
说明:
分隔时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:
输入: s = “catsanddog” wordDict = [“cat”, “cats”, “and”, “sand”, “dog”]
输出: [ “cats and dog”, “cat sand dog” ]
示例 2:
输入: s = “pineapplepenapple” wordDict = [“apple”, “pen”, “applepen”,“pine”, “pineapple”]
输出: [ “pine apple pen apple”, “pineapple pen apple”, “pine applepen apple” ]
解释: 注意你可以重复使用字典中的单词。
示例 3:
输入: s = “catsandog” wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
输出: []
解答
在单词拆分I的基础上进行动态规划,和以往vector使用push_back()、pop_back()的回溯方式不同,这里巧妙的使用了将原str赋值给新的nextstr后再进行append(),之后str还是原先的str无需回溯。动态规划部分代码如下:
void dfs(vector<string>& result, string s, string str, vector<string>& wordDict) {
if(s.length() == 0){
result.push_back(str);
}
else {
for(int i = 1; i <= s.length(); i++) {
string subs = s.substr(0, i);
if(find(wordDict.begin(), wordDict.end(), subs) != wordDict.end()) {
string nextstr = str;
if(nextstr.length()) {
nextstr.append(" ");
}
nextstr.append(subs);
dfs(result, s.substr(i, s.length()-i), nextstr, wordDict);
}
}
}
}
总代码:
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
int n = s.length();
vector<bool> isbreak(n+1, false);
isbreak[0] = true;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= i; j++) {
if(isbreak[j] && find(wordDict.begin(), wordDict.end(), s.substr(j,i-j)) != wordDict.end()) {
isbreak[i] = true;
}
}
}
vector<string> result;
if(isbreak[n]){
string str;
dfs(result, s, str, wordDict);
}
return result;
}
void dfs(vector<string>& result, string s, string str, vector<string>& wordDict) {
if(s.length() == 0){
result.push_back(str);
}
else {
for(int i = 1; i <= s.length(); i++) {
string subs = s.substr(0, i);
if(find(wordDict.begin(), wordDict.end(), subs) != wordDict.end()) {
string nextstr = str;
if(nextstr.length()) {
nextstr.append(" ");
}
nextstr.append(subs);
cout << i << " " << nextstr << endl;
dfs(result, s.substr(i, s.length()-i), nextstr, wordDict);
}
}
}
}
};
输出: