题目:
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
分析:
唉,感觉不是很认真。。。
代码:
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
if(wordDict.size()==0)return false;
vector<bool> rec(s.size()+1,false);
rec[0]=true;
for(int i=1;i<=s.size();++i)
for(int j=i-1;j>=0;--j)
if(rec[j]&&wordDict.find(s.substr(j,i-j))!=wordDict.end()){
rec[i]=true;
break;
}
return rec[s.size()];
}
};