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

 public class Solution {
public List<String> wordBreak(String s, Set<String> dict) {
List<String> result=new ArrayList<String>();
if(!wordBreakPossible(s,dict)) return result;
dfs(s,dict,result,"",0);
return result;
} private void dfs(String s, Set<String> dict, List<String> result,String temp, int start) {
// TODO Auto-generated method stub
if(start==s.length())
result.add(temp);
else{
if(start!=0)
temp+=" ";
for(int i=start;i<s.length();i++){
String word=s.substring(start, i+1);
if(dict.contains(word))
dfs(s,dict,result,temp+word,i+1);
}
}
} 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()];
}
}

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

20150221:

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

导致出现了TLE:

Last executed input: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
 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()];
}
}

[Leetcode] Word BreakII的更多相关文章

  1. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  3. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  4. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  5. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  6. [LeetCode] Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. [LeetCode] Word Frequency 单词频率

    Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...

  8. [LeetCode] Word Break II 拆分词句之二

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

  9. [LeetCode] Word Break 拆分词句

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. devices-list

    转自:https://www.kernel.org/pub/linux/docs/lanana/device-list/devices-2.6.txt LINUX ALLOCATED DEVICES ...

  2. test1.A[【dfs简单题】

    Test1.A Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 sdut 2274:http://acm.sdut.edu.cn/ ...

  3. dbca:Exception in thread "main" java.lang.UnsatisfiedLinkError: get

    在64位的操作系统安装oracle10g 软件安装完成后,使用dbca建库的时候报下面的错: $ dbcaUnsatisfiedLinkError exception loading native l ...

  4. MDX语法之排序函数Order

    使用场景: 排列指定集的成员,可以选择保留或打乱原有的层次结构. 语法: Numeric expression syntax Order(Set_Expression, Numeric_Express ...

  5. 6-05使用SQL语句删除数据

    删除数据语法: DELETE  FROM 表名  WHERE  删除条件. TRUNCATE  TABLE 表名. --[1]基本删除,省略WHERE条件,将删除表中的所有数据 DELETE FROM ...

  6. [Eclipse] Eclipse配置Tomcat插件

    1 . Eclipse IDE 3.6 for Java EE Developersat- 5.5.28 或者以上版本 : 2 . 安装 Tomcat 插件 , 文件名: tomcatPluginV3 ...

  7. 如何在ASP.NET 5中使用ADO.NET

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:ASP.NET 5是一个全新的平台,在这个平台上也带来一些全新的函数库.不过这并非意味 ...

  8. RFC(请求注解)--各种协议-标准

    转自:http://blog.sina.com.cn/s/blog_65d6476a0101cj8n.html RFC(Request For Comments)-意即“请求注解”,包含了关于Inte ...

  9. WPF之MVVM(Step1)——自己实现ICommand接口

    开发WPF应用程序,就不得不提MVVM.下面偶将展示MVVM中简单的实现,其中主要在于ICommand的实现上,不过这种实现方式,应该不会有多少人在开发中使用,在此仅作学习使用. 准备: 界面绘制,简 ...

  10. 四种方案解决ScrollView嵌套ListView问题(转)

    以下文章转自@安卓泡面 在工作中,曾多次碰到ScrollView嵌套ListView的问题,网上的解决方法有很多种,但是杂而不全.我试过很多种方法,它们各有利弊. 在这里我将会从使用ScrollVie ...