题目:

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. java中的拷贝文件FileChannel

    以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码: 下边是传统的byte数组拷贝方法 </pre><pre name=&q ...

  2. 命令行创建AVD

    1.命令行创建AVD android create avd -n myAvd -t 8 -b armeabi-v7a -p d:\scard.img -s HVGA 2.删除AVD android d ...

  3. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

  4. (四)JAVA使用POI操作excel

    1,字体处理 Demo12.java package com.wishwzp.poi; import java.io.FileOutputStream; import org.apache.poi.h ...

  5. $.get

    $('#choice').change(function() { if($(this).val() != '') { $.get( 'data.php', { what: $(this).val() ...

  6. MBR与分区表备份与恢复

    常用工具列表 dd                   数据复制,转换实用工具 tar                  GNU磁盘存档实用工具 cpio                数据存档实用工 ...

  7. GNU iconv

    GNU iconv 一.关键函数 1.iconv_open() iconv_open(DestinationCharsets, SourceCharSets) 2.iconv() [XSI] [Opt ...

  8. C语言经典案例

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%:20万到40万 ...

  9. ubuntu16.04无法连接无线的问题解决方式以及QQ的安装

    0x01 首先我是安装了win10与ubuntu16.04的双系统,不过遇到的问题有启动项与无线连接的问题,今天说一下联网的问题. 连接宽带是正常的,只需要操作sudo pppoeconf 这条命令即 ...

  10. vmware以及schlumberger题解

    先是vmare的:具体的题目我就不描述了. 1. 贪吃的小明.直接数个数,统计个数,就可以完成.使用map,应该输入implement这一类,我认为很简单,但是我只过了33%. /* ID: y119 ...