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 ...
随机推荐
- 转:更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法
拜GFW所赐,对于初学者的我来说,总会出现一些莫名其妙的问题 首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本 ...
- 【转】 i2c驱动调试经验
原文网址:http://blog.csdn.net/cmm20071020/article/details/7179958 把一个i2c驱动从2.6.21升级到2.6.39 上网查到一篇帖子,讲了驱动 ...
- Php官方指导安装与配置
From:http://php.net/manual/zh/install.php 提示:如需在 Windows 平台设置并立即运行 PHP,您还可以:下载 WebMatrix 安装与配置 安装前需要 ...
- Controlling Access in Java
Referrence: Oracle Java Doc Two levels top level: public, or package-private (no explicit modifier) ...
- Spiral Matrix 解答
Question Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ...
- poj3299
...
- 第01讲- Android背景知识
第01讲Android背景知识 Android是基于Linux系统 Android系统框图 : 第一.操作系统层(OS) 第二.各种库(Libraries)和Android 运行环境(RunTime) ...
- C# yield return 流程理解
代码如下: 在Documents1方法中使用yield return之后, 下次在进入Documents1方法就是从上一次yield return部分执行 using System; using S ...
- IOS UIImage 模糊
#import <UIKit/UIKit.h> #import <Accelerate/Accelerate.h> #import <QuartzCore/QuartzC ...
- CSS---------------之文本颜色
CSS2支持如下名字的颜色 注意点: 你的浏览器有可能支持更多名字,但是在实际用的过程中尽量少使用名字的,因为各个浏览器对颜色的会存在差异:查看颜色可以参考 对于更多地颜色,你可以使用代表红,绿,蓝三 ...