import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
//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".
public class WordBreak {
public static void main(String[] args){
// boolean[] a=new boolean[2];
// System.out.print(a[1]);
Set
b=new HashSet();
b.add("b");
b.add("abc");
b.add("b");
b.add("cd");
System.out.print(wordBreak("abcd",b));
}
public static boolean wordBreak(String s, Set dict) {
if (s==null)
return true;
if(dict.size()==0)
return false;
int n =s.length();
boolean[] dp=new boolean[n+1];
dp[0]=true;
for(int i=1;i<=n;i++){
if(dp[i-1]){
int idx=i-1;
for(int j=idx;j
String cur=s.substring(idx,j+1);
if(dict.contains(cur)){
dp[j+1]=true;
}
}
}
}
// System.out.print(dp[0]);
return dp[n];
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class WordBreak2 {
public List wordBreak(String s, Set dict) {
List temp=new ArrayList();
List b=new ArrayList();
b=firstmatch(s,dict);
// for(int i=0;i<=)
return temp;
}
public List firstmatch(String s, Set dict){
String[] a=(String[])dict.toArray();
List temp=new ArrayList();
for(int i=0;i
if(s.substring(0, a[i].length()).equals(a[i])){
temp.add(a[i]);
}
}
return temp;
}
}
阅读(300) | 评论(0) | 转发(0) |