[Leetcode] Word BreakII

Question:

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

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

----------------------------------------------

Solution:

dfs.

但是需要注意的是,在dfs之前,需要判断这个string能不能被这个dictionary分割(Word Break)。

 1 public class Solution {
 2     public List<String> wordBreak(String s, Set<String> dict) {
 3         List<String> result=new ArrayList<String>();
 4         if(!wordBreakPossible(s,dict)) return result;
 5         dfs(s,dict,result,"",0);
 6         return result;
 7     }
 8 
 9     private void dfs(String s, Set<String> dict, List<String> result,String temp, int start) {
10         // TODO Auto-generated method stub
11         if(start==s.length())
12             result.add(temp);
13         else{
14             if(start!=0)
15                 temp+=" ";
16             for(int i=start;i<s.length();i++){
17                 String word=s.substring(start, i+1);
18                 if(dict.contains(word))
19                     dfs(s,dict,result,temp+word,i+1);
20             }            
21         }
22     }
23 
24     private boolean wordBreakPossible(String s, Set<String> dict) {
25         // TODO Auto-generated method stub
26         boolean[] state=new boolean[s.length()+1];
27         state[0]=true;
28         for(int i=1;i<=s.length();i++){
29             for(int j=i-1;j>=0;j--){
30                 if(state[j]&&dict.contains(s.substring(j, i))){
31                     state[i]=true;
32                     break;
33                 }
34                 
35             }
36         }
37         return state[s.length()];
38     }
39 }

 ----------------------------------------------------------------------

20150221:

又忘了在dfs之前去判断这个string是否可以被这个dictionary分割:

导致出现了TLE:

Last executed input: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
 1 public class Solution {
 2     public List<String> wordBreak(String s, Set<String> dict) {
 3         
 4         List<String> res=new ArrayList<String>();
 5         if(s==null||s.length()==0||dict==null||dict.size()==0)
 6             return res;
 7         if(!wordBreakPossible(s,dict)) return res;
 8         dfs(res,s,dict,"");
 9         return res;
10     }
11     public void dfs(List<String> res,String s,Set<String> dict,String temp){
12         if(s.length()==0){
13             
14             res.add(temp.trim());
15             return;
16         }
17         for(int i=1;i<=s.length();++i){
18             String t=s.substring(0,i);
19             if(dict.contains(t)){
20                 dfs(res,s.substring(i),dict,temp+" "+t);
21             }else{
22                 continue;
23             }
24         }
25     }
26     private boolean wordBreakPossible(String s, Set<String> dict) {
27         // TODO Auto-generated method stub
28         boolean[] state=new boolean[s.length()+1];
29         state[0]=true;
30         for(int i=1;i<=s.length();i++){
31             for(int j=i-1;j>=0;j--){
32                 if(state[j]&&dict.contains(s.substring(j, i))){
33                     state[i]=true;
34                     break;
35                 }
36                 
37             }
38         }
39         return state[s.length()];
40     }
41 }

 

posted @ 2014-06-12 15:32  Phoebe815  阅读(296)  评论(2编辑  收藏  举报