您的位置:首页 > 其它

leetcode 139. Word Break

2017-10-19 12:02 190 查看
题目:

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"
.

思路:
http://blog.csdn.net/gao1440156051/article/details/52192981
设dp[i]为前i个字符是否可以切割。

则dp[i]=dp[j]&&s.substr(j,i-j)

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len=s.size();
vector<bool> dp(len+1,false);
dp[0]=true;
for(int i=1;i<=len;i++){
for(int j=i-1;j>=0;j--){
if(dp[j]&&wordDict.count(s.substr(j,i-j))){
dp[i]=true;
break;
}
}
}
return dp[len];
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: