Word Break I II
Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
解法1,递归,超时
有两种递归方式,一种是按s的子串递归,一种是按集合dict递归
第一种:
public boolean wordBreak(String s, Set<String> dict) {
if(dict.contains(s)){
return true;
}
boolean flag = false;
for(int i=1;i<s.length();i++){
if(dict.contains(s.substring(0,i))){
flag = wordBreak(s.substring(i),dict);
}
}
return flag;
}
第二种:
public boolean wordBreak(String s, Set<String> dict) {
if(dict.contains(s)||s.equals("")){
return true;
}
boolean flag = false;
for(String now:dict){
for(int i = 0;i<s.length()-now.length();i++){
if(s.substring(i, i+now.length()).equals(now)){
flag = wordBreak(s.substring(0,i), dict)&&wordBreak(s.substring(i+now.length()), dict);
if(flag){
return true;
}
}
}
}
return flag;
}
解法2:动态规划
设flag[]为boolean数组,flag[i]表示s.substring(0,i)是否满足wordbreak,因此有状态转移方程为:
flag[n] = ∑flag[i]&&s.substring(i,n) (i 取值为从1到n-1),有代码如下:
public boolean wordBreak(String s, Set<String> dict) {
boolean flag[] = new boolean[s.length()+1];
flag[0] = true;
for(int i=1;i<=s.length();i++){
for(int j=0;j<i;j++){
flag[i] = flag[i] || (flag[j]&&dict.contains(s.substring(j, i)));
if(flag[i]){
continue;
}
}
}
return flag[s.length()];
}

递归的复杂度为O(2^n),动态规划的复杂度为O(n^2)
个人感觉,动态规划版本就是递归中第一种解法的非递归版,递归是自顶向下计算,动态规划是自底向上计算。
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"].
做法与上面相似,boolean数组变成list<Integer>的数组,记录当前位可以从前面哪些位得出,先自底向上生成数组,然后自顶向下构造字符串,直接贴代码:
private List<String> result = new ArrayList<String>();
public List<String> wordBreak(String s, Set<String> dict) {
List<List<Integer>> flag = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
temp.add(0);
flag.add(temp);
for (int i = 1; i <= s.length(); i++) {
temp = new ArrayList<Integer>();
for (int j = 0; j < i; j++) {
if (dict.contains(s.substring(j, i)) && flag.get(j).size() > 0) {
temp.add(j);
}
}
flag.add(temp);
}
genList(flag, s, "", flag.get(flag.size() - 1), flag.size() - 1);
return result;
} private void genList(List<List<Integer>> flag, String source, String now,
List<Integer> list, int pos) {
// TODO Auto-generated method stub
if (pos == 0) {
result.add(now);
return;
}
for (Integer i : list) {
String temp = "";
if (now.equals("")) {
temp = source.substring(i, pos); } else {
temp = source.substring(i, pos) + " " + now;
}
genList(flag, source, temp, flag.get(i), i.intValue());
}
}

Word Break I II的更多相关文章
- LeetCode: Word Break I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- 【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 ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- [LeetCode] Word Break II 解题思路
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】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: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
随机推荐
- javascript运算符整理
说起运算符,基本上各类编程语言中都会涉及,使用方法大同小异.今天在这里以javascript做简单的整理. 总得来说运算符还是比较的多,大致可以分为以下几种类型: 一元运算符 位运算符 布尔运算符 乘 ...
- C# -- 扩展方法的应用(Extension Methods)
当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添加功能,我们想到的就是在类A中添加公共方法,这个显而易见肯定可以,但是由于某种原因,你不能修改类A本身的代码,但是确实又需要增加 ...
- RedHat安装GCC问题-解决依赖问题
RedHat Linux在安装gcc时需要cpp和cloog-ppl但是在安装cpp的时候需要这个依赖:libmpfr.so.1()(64bit) is needed by cpp-4.4.6-3.e ...
- [Hapi.js] Friendly error pages with extension events
hapi automatically responds with JSON for any error passed to a route's reply()method. But what if y ...
- 推荐几个常用的jquery ui 框架
jQuery ui框架很多,除了官方提供的jquery UI(如果你还不知道什么是jQuery UI,请看下载了jquery ui后如何使用),还有很多第三方提供的ui框架,因官方提供的jquery ...
- Git应用于Android项目的入门知识:我的理解
Git应用于Android项目的基本知识. 常常将git,repo和gerrit三种工具配合起来使用,使Android开发中的部分工作自动化.并适应敏捷项目管理的需要. repo是Go ...
- iOS-设计模式之代理反向传值
代理设计模式就是自己的方法自己不实现,让代理对象去实现. 可以让多个类实现一组方法. 委托模式的好处在于: 1.避免子类化带来的过多的子类以及子类与父类的耦合 2.通过委托传递消息机制实现分层解耦 代 ...
- Masonry的一些使用。
除了等距的有问题(懒得改了),其他用过挺正常的,我自己也是刚使用,有问题还请海涵. 地址:http://pan.baidu.com/s/1boyxu8Z
- stretchlim函数分析
在看imadjust代码时,看到stretchlim函数,特此分析一下,代码注释如下 function lowhigh = stretchlim(varargin) %STRETCHLIM Find ...
- DOM 对象之 document.all
1.document.all是页面内所有元素的一个集合: 2.经测试在chrome,safari,opera,ie中均返回一个HTMLALLCollection[xx]对象,在FF中返回是一个unde ...