题目来源:leetcode
Word Break I
Word Break I
Given a non-empty string s and a dictionary wordDict containing a list of
non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
思路一:递归(大数据超时)
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
boolean res=false;
if(s.length()==0)
return false;
char c=s.charAt(0);
for(String seq:wordDict) {
if (seq.length() <= s.length()) {
if (c == seq.charAt(0)) {
int i = 0;
for (; i < seq.length(); i++) {
if ( s.charAt(i) != seq.charAt(i))
break;
}
if (i==s.length())
return true;
else if (i == seq.length()) {
res = wordBreak(s.substring(seq.length(), s.length()), wordDict);
if(res==true)
return res;
}
}
}
}
return res; }
}
思路二:动态规划;dp记录每加入一个字符的是否可以分割 (怎么想不到呢??)
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
boolean dp[]=new boolean[s.length()+1];
dp[0]=true;
for(int i=1;i<=s.length();i++)
for(int j=0;j<i;j++){
if(dp[j]&&wordDict.contains(s.substring(j,i)))
{dp[i]=true;
break;
}
}
return dp[s.length()];
}
}
Word Break I I
Given a non-empty string s and a dictionary wordDict containing a list of
non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
思路:列举出来递归 没A过,不想改了
/**
* Created by a819 on 2017/8/19.
*/
import java.util.*;
public class wordBreakII {
public List<String> wordBreak(String s, List<String> wordDict) {
ArrayList<String> res = new ArrayList<>();
StringBuilder sb=new StringBuilder();
//boolean res=false;
if (s.length() == 0)
return res;
res=wordBreakI(s,wordDict,sb,res);
/* for(int i=res.size()-1;i>=0;i--){
res.add(res.get(i));
}*/
return res; }
public ArrayList<String> wordBreakI(String s, List<String> wordDict, StringBuilder sb,ArrayList<String> ls) {
char c = s.charAt(0);
for (String seq : wordDict) {
if (seq.length() <= s.length()) {
if (c == seq.charAt(0)) {
int i = 0;
for (; i < seq.length(); i++) {
if (s.charAt(i) != seq.charAt(i))
break;
}
if (i == s.length()) {
sb.append(" "+seq);
sb.delete(0,1);
ls.add(sb.toString());
//sb=new StringBuilder();
return ls;
}
else if (i == seq.length()) {
sb.append(" "+seq);
ls= wordBreakI(s.substring(seq.length(), s.length()), wordDict,sb,ls);
sb=new StringBuilder();//每生成一个片段要及时清空,否则会加到下一个片段
}
}
}
}
return ls;
}
public static void main(String[] args) {
String s="aaaaaaa";
String []a=new String[]{"aaaa","aa","a"};
wordBreakII w=new wordBreakII();
System.out.println(w.wordBreak(s,Arrays.asList(a)).size());
}
}
正确:而且需要剪纸
public class Solution {
public List<String> wordBreak(String s, Set<String> dict) {
List<String> res=new ArrayList<String>();
if(s==null||s.length()==0||dict==null||dict.size()==0)
return res;
if(!wordBreakPossible(s,dict)) return res;
dfs(res,s,dict,"");
return res;
}
public void dfs(List<String> res,String s,Set<String> dict,String temp){
if(s.length()==0){
res.add(temp.trim());
return;
}
for(int i=1;i<=s.length();++i){
String t=s.substring(0,i);
if(dict.contains(t)){
dfs(res,s.substring(i),dict,temp+" "+t);
}else{
continue;
}
}
}
private boolean wordBreakPossible(String s, Set<String> dict) {
// TODO Auto-generated method stub
boolean[] state=new boolean[s.length()+1];
state[0]=true;
for(int i=1;i<=s.length();i++){
for(int j=i-1;j>=0;j--){
if(state[j]&&dict.contains(s.substring(j, i))){
state[i]=true;
break;
}
}
}
return state[s.length()];
}
}