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. 与你相遇好幸运,The Moe Node.js Code Style Guide

    The Moe Node.js Code Style Guide  By 一个最萌的开发者 @2016.9.21 >>代码是人来阅读的,格式规范的代码是对编程人员最好的礼物 :) > ...

  2. mipi和dsi

    转自: http://blog.csdn.net/longxiaowu/article/details/24410021 一.MIPI MIPI(移动行业处理器接口)是Mobile Industry ...

  3. 【翻译二十三】java-并发程序之随机数和参考资料与问题(本系列完)

    Concurrent Random Numbers In JDK 7, java.util.concurrent includes a convenience class, ThreadLocalRa ...

  4. hdu 4759 大数+找规律 ***

    题目意思很简单. 就是洗牌,抽出奇数和偶数,要么奇数放前面,要么偶数放前面. 总共2^N张牌. 需要问的是,给了A X B Y  问经过若干洗牌后,第A个位置是X,第B个位置是Y 是不是可能的. Ja ...

  5. OpenMesh 删除网格顶点

    OpenMesh 提供了 delete_vertex() 函数来实现从网格中删除顶点,在删除掉顶点的同时,所有与该顶点相连的边也同时被删除. OpenMesh 官方文档 中给的顶点删除函数声明如下: ...

  6. leetcode 1_2_3_7

    来自lknny.com,欢迎交流学习!点击 tags: [leetcode,algorithm] categories: algorithm --- Two Sum Given an array of ...

  7. Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树

    E. Sign on Fence   Bizon the Champion has recently finished painting his wood fence. The fence consi ...

  8. MySQL中的约束简单使用

    数据库约束是为了保证数据的完整性而实现的一套机制,它具体的根据各个不同的数据库的实现而有不同的工具.一般来说有以下几种实现方式:1.检查约束:通过在定义数据库表里,在字段级或者是在表级加入的检查约束, ...

  9. Arduino101学习笔记(八)—— 函数库

    /*********最小值*********/ min() //实现:#define min(a,b) ((a)<(b)?(a):(b)) /*********最大值*********/ max ...

  10. 【tomcat 无法部署】svn上下载的maven项目无法部署到tomcat中

    问题: svn上下载的maven项目无法部署到tomcat中,tomcat不识别项目,但是这个项目确实是web项目 发现的过程: 然后依次产看项目的编译版本: 项目的依赖架包: 才发现: 解决方法: ...