题目描述
给定一个非空字符串 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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-break
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路一
深度优先搜索每一种可能的结果。为了避免重复搜索,用一个hashTable记录以当前字符为首的字符串能否被空格拆分为一个或多个在字典中出现的单词。
代码(c++)
class Solution {
public:
bool DFS(string s,map<string,int> &mp,int start,int hashTable[],int l_len,int m_len){
if(start==s.length()){
return true;
}
if(hashTable[start]!=-1){
return hashTable[start]==1?true:false;
}
for(int L=l_len;L<=m_len;L++){
if(start+L>s.length()) return false;
if(mp.find(s.substr(start,L))!=mp.end()&&DFS(s,mp,start+L,hashTable,l_len,m_len)){
hashTable[start]=1;
return true;
}
}
hashTable[start]=0;
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
map<string ,int> mp;
int l_len=INT_MAX;
int m_len=0;
for(int i=0;i<wordDict.size();i++){
int word_len=wordDict[i].length();
l_len=min(l_len,word_len);
m_len=max(m_len,word_len);
mp[wordDict[i]]=1;
}
int start=0;
int hashTable[s.length()];
memset(hashTable,-1,sizeof(hashTable));
return DFS(s,mp,start,hashTable,l_len,m_len);
}
};
思路二
动态规划。用dp[i]记录以字符i-1结尾的子字符串能否被空格拆分为一个或多个在字典中出现的单词。遍历每一个字符,检查以当前字符为尾的子字符串中每一个字符的dp[j]是否为true,以及 j 到 i-j 的子串是否在字典中,如果同时成立,则dp[i]为true,如果遍历完所有字符都不能同时成立,则dp[i]为false。
代码(c++)
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.length()+1,false);
dp[0]=true;
map<string,int> mp;
for(int i=0;i<wordDict.size();i++){
mp[wordDict[i]]=1;
}
for(int i=1;i<=s.length();i++){
for(int j=0;j<i;j++){
if(dp[j]&&mp.find(s.substr(j,i-j))!=mp.end()){
dp[i]=true;
break;
}
}
}
return dp[s.length()];
}
};
单词拆分 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”]
输出:
[]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-break-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
深度优先搜索每一种可能的结果。为了避免重复搜索,在递归的每一层记录以当前结点为首元素的字符串所有可能的分隔结果,如果再次搜索到该字符,则直接返回结果。
代码(c++)
class Solution {
public:
vector<string> DFS(string s,map<string ,int> &mp,map<int,vector<string> > &mp_sub,int start,int l_len,int m_len){
if(mp_sub.find(start)!=mp_sub.end()){
return mp_sub[start];
}
vector<string> cur;
if(start==s.length()){
cur.push_back("");
return cur;
}
for(int L=l_len;L<=m_len;L++){
if(start+L>s.length()) break;
if(mp.find(s.substr(start,L))!=mp.end()){
vector<string> post=DFS(s,mp,mp_sub,start+L,l_len,m_len);
for(int i=0;i<post.size();i++){
cur.push_back(s.substr(start,L)+(post[i]==""?"":" ")+post[i]);
}
}
}
mp_sub[start]=cur;
return cur;
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
map<string ,int> mp;
int l_len=INT_MAX;
int m_len=0;
for(int i=0;i<wordDict.size();i++){
int word_len=wordDict[i].size();
l_len=min(l_len,word_len);
m_len=max(m_len,word_len);
mp[wordDict[i]]=1;
}
int start=0;
string temp;
map<int,vector<string> > mp_sub;
return DFS(s,mp,mp_sub,start,l_len,m_len);
}
};