题目:

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

链接: http://leetcode.com/problems/word-break-ii/

题解:

又看到这种求所有解集的题目,自然就想到用DFS + Backtracking。在Word Ladder II里我们这样做,在这里我们依然这样做。感觉现在DFS往往伴随Backtracking,以后面试题估计这是一种常态。 这里需要注意的是对有个超长的case,我们要提前判断能否word break,所以还要用带一部分Word Break I里面的代码。  为图省事直接copy & paste了,其实应该还能重构一下,让代码不那么sloppy。回溯的部分依然是Word Break I的结构,使用了一个StringBuilder来回溯加入的单词以及空格。这回时间复杂度是真的O(2n)了。

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
public List<String> wordBreak(String s, Set<String> wordDict) {
List<String> res = new ArrayList<>();
if(s == null || wordDict == null)
return res;
StringBuilder sb = new StringBuilder();
if(canWordBreak(s, new HashSet<String>(wordDict))) //find out if we can word break
findAllWordBreak(res, sb, s, wordDict);
return res;
} private void findAllWordBreak(List<String> res, StringBuilder sb, String s, Set<String> wordDict) {
if(s.length() == 0) {
res.add(sb.toString().trim());
return;
} for(int i = 1; i <= s.length(); i++) {
String frontPart = s.substring(0, i);
String backPart = s.substring(i, s.length());
if(wordDict.contains(frontPart)) {
sb.append(frontPart);
sb.append(" ");
findAllWordBreak(res, sb, backPart, wordDict);
sb.setLength(sb.length() - 1 - frontPart.length());
}
}
} private boolean canWordBreak(String s, Set<String> wordDict) { //Word Break I
if(s == null || wordDict == null)
return false;
if(s.length() == 0)
return true; for(int i = 1; i <= s.length(); i++) {
String frontPart = s.substring(0, i);
String backPart = s.substring(i, s.length());
if(wordDict.contains(frontPart)) {
if(canWordBreak(backPart, wordDict))
return true;
wordDict.remove(frontPart);
}
} return false;
}
}

Reference:

https://leetcode.com/discuss/27464/my-concise-answer

https://leetcode.com/discuss/23770/slightly-modified-dp-java-solution

https://leetcode.com/discuss/7439/this-accepted-java-version-program-there-any-better-solution

https://leetcode.com/discuss/133/is-there-better-solution-for-this-word-breakii

http://www.cnblogs.com/springfor/p/3877056.html

http://blog.csdn.net/linhuanmars/article/details/22452163

http://www.programcreek.com/2014/03/leetcode-word-break-ii-java/

http://www.acmerblog.com/word-break-ii-6128.html

140. Word Break II的更多相关文章

  1. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  2. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  3. 【LeetCode】140. Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  4. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  5. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  6. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  7. leetcode 140. Word Break II ----- java

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

  8. Java for LeetCode 140 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笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...

随机推荐

  1. 将Cent0S 7的网卡名称eno16777736改为eth0

    新建的虚拟机redhat linux7默认的网卡名称是eno16777736,找不到eth0如图所示: 修改网卡名称: 输入如下命令,进入对应目录,编辑文件: vim /etc/sysconfig/g ...

  2. asp.net连接mysql数据库

    方法一:使用MySQL推出的MySQL Connector/Net组件, 该组件是MySQL为ADO.NET访问MySQL数据库设计的.NET专用访问组件.完成该组件后,需要在项目中引用这个组件,也可 ...

  3. xp 中的IIS安装成功之后,访问网页显示没有权限访问解决方法

    在做xp的IIS发布网站时遇到一个问题就是当你访问网站的时候,显示没有权限访问网站,但是我已经开启了匿名访问网站了,怎么还没有权限访问呢?后来经过上网搜资料解决,当时很多网上都说没打开匿名访问,当时我 ...

  4. GNU glibc

    在线G-lib-c(GNU C Library库)网站 参考: 1.bitsToTypes

  5. ThreadPoolExecutor 线程池的实现

    ThreadPoolExecutor继承自 AbstractExecutorService.AbstractExecutorService实现了 ExecutorService 接口. 顾名思义,线程 ...

  6. mysql 删除日志

    mysql日志过大,想用rm 删除掉,后来想一下,是不是有别的方法,搜索一下,果然有..... mysql > PURGE MASTER LOGS BEFORE '2014-03-16 00:0 ...

  7. html5 meta头部设置

    <meta name="viewport" content="height=[pixel_value | device-height], width=[pixel_ ...

  8. iPad知识点记录

    这两天玩了玩虚拟机安装Mac OS系统.iPad1的越狱以及利用iTunes将iPad2的系统升级到iOS8.1,这里将一些参考资源以及关键点记录一下. 一.虚拟机安装Mac OS 首先你的系统要能够 ...

  9. div之间有间隙以及img和div之间有间隙的原因及解决方法

    原因: div 中 存在 img标签,由于img标签的 display:inline-block 属性. display:inline-block布局的元素在chrome下会出现几像素的间隙,原因是因 ...

  10. javascript高级编程笔记02(基本概念)

    ParseInt()函数: 由于Number函数在转换字符串时比较复杂而且不合理,我们常常转换字符串都用parseInt函数, Parseint函数规则: 忽略字符串前面的空格,直到找到第一个非空格字 ...