140. Word Break II
题目:
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的更多相关文章
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 【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 ...
- [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 ...
- 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 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 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 ...
- 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 ...
- LeetCode笔记:140. Word Break II
题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...
随机推荐
- c++Builder 下的文件及目录操作
转自 http://blog.csdn.net/ktcserver/article/details/936329 一.判断目录是否存在: C++ Builder中提供了检查文件 ...
- javaee学习-新建servlet 直接返回html
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- 伪分布式环境下命令行正确运行hadoop示例wordcount
首先确保hadoop已经正确安装.配置以及运行. 1. 首先将wordcount源代码从hadoop目录中拷贝出来. [root@cluster2 logs]# cp /usr/local/h ...
- QQ登录网站接入
QQ网站登录是一个非常常用的功能,网上有很多的资料,在此只做一个整理: QQ登录接入也在不断的升级,目前我发布的是2.1,很多资料里显示的那些繁杂的步骤已经不需要了: 第一步需要先申请,申请地址如下: ...
- QtSQL学习笔记(4)- 使用SQL Model类
除了QSqlQuery,Qt提供了3个高级类用于访问数据库.这些类是QSqlQueryModel.QSqlTableModel和QSqlRelationalTableModel. 这些类是由QAbst ...
- hybird app
Hybrid App 是混合模式应用的简称,兼具 Native App 和 Web App 两种模式应用的优势,开发成本低,拥有 Web 技术跨平台特性.目前大家所知道的基于中间件的移动开发框架都是采 ...
- dataTable 禁止分页
$("#id").DataTable({ "paging": false, // 禁止分页 });
- Fluid Shopping Website 开发阶段性总结——第一周
开发目的: 可链接微信公众号,无论是桌面端.移动端完美兼容,给用户提供不逊于原生App的用户体验.作为一个软件,有充分的可扩展性,便于未来增强开发.同时给一些正在尝试做OTO的朋友们提供一个平台,因为 ...
- easy-ui datagrid
Easy-ui引用 <link href="css/EasyUI/themes/icon.css" rel="stylesheet" type=&q ...
- Smlusm随笔目录索引
1.MVVM Light Toolkit ) [转]Mvvm Light Toolkit for wpf/silverlight系列之搭建mvvmlight开发框架 2) [转]Mvvm Light ...