Word Break II leetcode java
题目:
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"].
题解:
这道题不仅仅是看是不是wordbreak,还需要在此基础上把所有word break的结果保存。
为了把所有可能性都保存,那么就使用DFS方法来解决。DFS主要就是跳的层次不容易看出,我下面就以字符串leetcode字典le l et eet code作为例子画了一张图,大概讲解了如何递回和返回,这样更加有助于理解。

代码如下:
1 public boolean wordBreakcheck(String s, Set<String> dict) {
2 if(s==null || s.length()==0)
3 return true;
4 boolean[] res = new boolean[s.length()+1];
5 res[0] = true;
6 for(int i=0;i<s.length();i++){
7 StringBuilder str = new StringBuilder(s.substring(0,i+1));
8 for(int j=0;j<=i;j++){
9 if(res[j] && dict.contains(str.toString())){
res[i+1] = true;
break;
}
str.deleteCharAt(0);
}
}
return res[s.length()];
}
public ArrayList<String> wordBreak(String s, Set<String> dict) {
ArrayList<String> res = new ArrayList<String>();
if(s==null || s.length()==0)
return res;
if(wordBreakcheck(s,dict))
helper(s,dict,0,"",res);
return res;
}
private void helper(String s, Set<String> dict, int start, String item, ArrayList<String> res){
if(start>=s.length()){
res.add(item);
return;
}
StringBuilder str = new StringBuilder();
for(int i=start;i<s.length();i++){
str.append(s.charAt(i));
if(dict.contains(str.toString())){
String newItem = new String();
if(item.length()>0)
newItem = item + " " + str.toString();
else
newItem = str.toString();
helper(s,dict,i+1,newItem,res);
}
}
}
Reference: http://blog.csdn.net/linhuanmars/article/details/22452163
Word Break II leetcode java的更多相关文章
- Word Break II——LeetCode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Word Ladder II leetcode java
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- Word Break II -- leetcode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- LeetCode: 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 Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 【leetcode】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
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
随机推荐
- Bzoj4710 分特产(容斥原理+组合数)
题面 Bzoj 题解 考虑容斥原理,所有人都有特产的方案数等于: 至少零个人没有特产\(-\)至少一个人没有特产\(+\)至少两个人有特产\(-...\) 接着考虑其中一种情况怎么求(假设现在至少有\ ...
- Java 关于集合框架那点事儿
1.引入集合框架 采用数组存在的一些缺陷: 1.数组长度固定不变,不能很好地适应元素数量动态变化的情况. 2.可通过数组名.length获取数组的长度,却无法直接获取数组中真实存储的个数. ...
- [代码审计]某开源商城前台getshell
0x00 前言 这套系统搞了有点久了,漏洞是发现了,但一直卡在某个地方迟迟没拿下来. 下面就分享一下自己审这套系统的整个过程. 0x01 系统简介 略 0x02 审计入口 看到inc\functi ...
- 常用SQL脚本记录一
20.SUM()和 列+ 统计结果时:如果列里有一行为null,SUM函数会忽略它:如果+,则结果集也为NULL了 19 SUBSTRING (expression,startIndex, endIn ...
- Beego 和 Bee 的开发实例
Beego不是一般的web开发包.它构建在大量已存在的Go之上,提供了许多的功能,以下是提供的功能: 一个完整的ORM 缓存 支持session 国际化(i18n) 实时监测和重载 发布支持 ==== ...
- bzoj 3275 最小割
给你一堆东西,叫你选一些东西出来,使得价值最大,要求选出的东西集合中的任意a,b满足性质p. 可以考虑: 1.拟阵? 2.二分图? 这道题由于数学硬伤,不知道不存在两条直角边是奇数,斜边是整数的直角三 ...
- Markdown中如何插入视频 > iframe?
关于Markdown中如何插入视频这一问题 网上众说纷纭,一直也没找到一个确切的答案,想来也是,这些东西毕竟还不算成熟.各种以前提供过的方法现在来讲,可能在更新或是关闭大潮中又没了 而且,Ma ...
- BZOJ 4521 CQOI 2016 手机号码 数位DP
4521: [Cqoi2016]手机号码 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 539 Solved: 325[Submit][Status ...
- 快速定位问题 Request无法获取参数
比如说最近开发甲修改了iframe标签的src,开发乙在设置src的时候传入了2个参数,通过iframe标签链接到这个页面时,开发乙调试时发现没有拿到任何参数值.然后开发乙百度了一下,发现iframe ...
- 数据表-java类的映射
1.一个数据表对应一个java类 2.数据表的字段对应java类的属性 3.一对多的数据表关系 一方用一个java对象表示 多方用一个java对象数组表示 4.多对多的数据表关系:采用中间表,将多对多 ...