您的位置:首页 > 其它

LeetCode139:Word Break

2015-06-07 21:40 351 查看
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"
.

思路:dp[i]=dp[j]+s.contais(s.substring(j,i+1));

public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {

int m=s.length();
boolean isSegmented[]=new boolean[m+1];
isSegmented[0]=true;
int start=0;
for(int i=0;i<m;i++)
for(int j=0;j<=i;j++){
isSegmented[i+1]=isSegmented[j]&&wordDict.contains(s.substring(j,i+1));
if(isSegmented[i+1])
break;
}
return isSegmented[m];
}

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: