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));

 1 public class Solution {
 2     public boolean wordBreak(String s, Set<String> wordDict) {
 3         
 4         int m=s.length();
 5         boolean isSegmented[]=new boolean[m+1];
 6         isSegmented[0]=true;
 7         int start=0;
 8         for(int i=0;i<m;i++)
 9         for(int j=0;j<=i;j++){
10            isSegmented[i+1]=isSegmented[j]&&wordDict.contains(s.substring(j,i+1));
11            if(isSegmented[i+1])
12            break;
13         }
14         return isSegmented[m];
15     }
16     
17 }
View Code

 

posted on 2015-06-07 21:40  gone~with~wind  阅读(106)  评论(0)    收藏  举报