class Solution {
public:
map<string,vector> mp;
vector dfs(string s,unordered_set &dict){
vector q;
if(dict.count(s))
{
q.insert(q.begin(),s);
}
for(int i=0;i<s.length();i++){
string poststr=s.substr(i);
if(dict.count(poststr)){
vector p=dfs(s.substr(0,i),dict);
for(int j=0;j<p.size();j++){
p[j]=p[j]+’ '+poststr;
}
q.insert(q.begin(),p.begin(),p.end());
}
}
mp[s]=q;
return q;
}
vector wordBreak(string s, unordered_set &dict) {
if(s.length()==0){
return vector();
}
vector result=dfs(s, dict);
return result;
}
};
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each
最新推荐文章于 2019-05-24 11:44:09 发布