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不重复,且每一个 ...
随机推荐
- sqlserver 变量
变量:分为全局变量和局部变量全部变量:以@@声明,为系统变量,所有实例都能访问,用户只能访问,不能赋值局部变量:生命周期只在一个批处理内有效, 局部变量经常使用的三种用途:1 在循环语句中记录循环的次 ...
- VS2012无法创建项目:未找到与约束……匹配的导出
故障情况:7月10号后用VS2012创建项目时,弹出如下对话框,无法创建新项目: 而后经网络搜索确定是7月10号更新了系统补丁后造成的 解决方案: 1.卸载这两个补丁后重启电脑: 2.到http:// ...
- asp.net连接mysql数据库
方法一:使用MySQL推出的MySQL Connector/Net组件, 该组件是MySQL为ADO.NET访问MySQL数据库设计的.NET专用访问组件.完成该组件后,需要在项目中引用这个组件,也可 ...
- JAVA访问配置文件总结
一.全局配置的简单 propertie 文件实现 package com.testgs.utils; import java.util.*; import java.io.*; public fina ...
- (转)TCP注册端口号大全
分类: 网络与安全 cisco-sccp 2000/tcp Cisco SCCPcisco-sccp 2000/udp Cisco SCCp# Dan Wing <dwing&cisco ...
- Express安装与调试
Express 是基于Node.Js平台,快速.开放.极简的 web 开发框架. 1.安装 Express的安装通过cmd来进行,过程如下: 首先,先在本地建立一个项目文件夹,取名Nodejs. 然后 ...
- JavaScript中的apply与call与arguments对象
(一) call方法 语法:presentObj.call(thisObj,arg1,arg2,arg3...) 参数thisObj :将被用作当前对象presentObj的对象. 当thisObj无 ...
- mysql innodb 数据打捞(二)innodb 页面打捞编程
有了页面的结构和特征,需要编程实现数据库页面的打捞工作: 为了方便windows and linux 的通用,计划做成C语言的控制台应用,并且尽量只用ansi c;关于多线程,计划做成多线程的程序,最 ...
- Web前端新人之CSS样式选择器
最近在学习css样式.那么我就想先整理一下css样式的选择器 规则结构: 每个规则都有两个基本部分:选择器和声明块.声明块由一个或者多个声明组成,每个声明则是一个属性—值对(property-valu ...
- WPF MVVM 用户控件完成分页
项目中经常会有分页查询的情况,在WPF中我们可以通过用户控件完成分页 一下为分页控件的页面代码, <UserControl x:Class="Foundation.UCtrl.Next ...