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 4.0.3 CTS 测试
原文网址:http://blog.csdn.net/zxm317122667/article/details/8508013 Android-CTS 4.0.3测试基本配置 1. Download C ...
- 【转】树莓派学习笔记——I2C Tools 学习笔记
原文网址:http://blog.csdn.net/xukai871105/article/details/15029843 1.安装 I2C驱动载入和速率修改请查看博文[树莓派学习笔记——I ...
- 【HDU1514】Stars(树状数组)
绝对大坑.千万记住树状数组0好下标位置是虚拟节点.详见大白书P195.其实肉眼看也能得出,在add(有的也叫update)的点修改操作中如果传入0就会死循环.最后TLE.所以下标+1解决问题.上代码! ...
- c++ 友元类
一.友元类相关概念 要将私有成员数据或函数暴露给另一个类,必须将后者声明为友元类. 注意三点: (1)友元关系不能传递 (2)友元关系不能继承 (3)友元关系不能互通
- c++在函数后面加const
非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为 ...
- vi命令笔记
vim编辑器 文本编辑器,字处理器ASCII nano, sed vi: Visual Interfacevim: VI iMproved 全屏编辑器,模式化编辑器 vim模式:编辑模式(命令模式)输 ...
- WPF弹出对话确认框
MessageBoxResult mr = CMessageBox.ShowQuestionMessage("点击“是”继续,点击“否”取消.", "确认删除?" ...
- JavaScript 基础一
内部: <Script Language="JavaScript" type="text/javascript"> JavaScript代码 < ...
- IOS 用drawRect 画表格
自定义一个View DrawLine DrawLine.h #import <UIKit/UIKit.h> @protocol gridTouchDelete <NSObject&g ...
- Linux正則表達式-反复出现的字符
星号(*)元字符表示它前面的正則表達式能够出现零次或多次.也就是说,假设它改动了单个字符.那么该字符能够在那里也能够不在那里,而且假设它在那里,那可能会不止出现一个.能够使用星号元字符匹配出如今引號中 ...