题目:

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. 安装.NET Framework组件时,电脑意外重启后再次安装失败

    因为软件运行环境需要安装.Net Framework,我安装的是2.0sp版本,可以安装过程中计算机意外关闭,重新打开后再次安装却出现安装失败的提示,具体内容是: 产品: Microsoft .NET ...

  2. Oracle连接配置以及实例的备份和恢复

    背景:一个团队项目开发,不可能每个人都架设自己本地的数据库,大多数情况下是统一用服务器上的数据库,这时候就需要进行远程数据库的连接.而且有时候还需要进行数据库搬迁 ,这时候就需要进行数据库的备份和恢复 ...

  3. 第三篇、CSS样式简介

    <!--1.行内样式 <p style="background-color:red;font-size:20px"> --> <!--2.页内样式 & ...

  4. freemark页面中获取list循环中的counter

    如何在freemark页面中获取到当前list循环的counter 直接上代码 <#list lists as x> <#assign j=x?counter> ${j} // ...

  5. NSMutableAttributedString(富文本)的简单使用

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  6. Objective-C 学习笔记(Day 3,下)

    ------------------------------------------- 封装概念及其原理 一个Gun类的例子来详细说明这一环节: #import <Foundation/Foun ...

  7. ZOJ 2411 Link Link Look(BFS)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1411 题目大意:连连看,给出每次连线的两个坐标,求能消去多少方块,拐 ...

  8. MFC GDI相关对象

    首先说明几个名词: CDC 是MFC对Wind32 API的设备上下文(DC)进行封装的C++类,由他继承的类包括 CPaintDC(常用)CWindowDC(现在软件基本不用) CClientDC( ...

  9. 使用AutoMapper

    一.AutoMapper初探: [参考Using AutoMapper: Getting Started] 1.新建空的ASP.NET MVC项目. 2.在Models文件夹添加类: public c ...

  10. Java中的面向对象

    Java中的面向对象 在软件开发的学习中, 我最先接触的开发语言就是java,但都是简单的函数和循环数组的应用.说道面向对象,第一次看到这个词的时候还是在C#的学习过程中,我记得当时PPT上霸气的解释 ...