Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode
这道题相似 Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案。
因此用递归。
可是直接递归做会超时,原因是LeetCode里有几个非常长可是无法拆分的情况。所以就先跑一遍Word Break,先推断能否拆分。然后再进行拆分。
递归思路就是,逐一尝试字典里的每个单词,看看哪一个单词和S的开头部分匹配,假设匹配则递归处理S的除了开头部分,直到S为空。说明能够匹配。
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"]
.
public class Solution {
public List<String> wordBreak(String s, Set<String> dict) {
List<String> list = new ArrayList<String>();
List<String> ret = new ArrayList<String>();
rec(s, dict, list, ret);
return ret;
} public void rec(String s, Set<String> dict, List<String> list, List<String> ret) {
if(!isBreak(s, dict)){ // test before run to avoid TLE
return;
}
if(s.length() == 0) {
String concat = "";
for(int i=0; i<list.size(); i++) {
concat += list.get(i);
if(i != list.size()-1) {
concat += " ";
}
}
ret.add(concat);
return;
} for(String cur : dict) {
if(cur.length() > s.length()) { // avoid out of boundary
continue;
}
String substr = s.substring(0, cur.length());
if(substr.equals(cur)) {
list.add(substr);
rec(s.substring(cur.length()), dict, list, ret);
list.remove(list.size()-1);
}
}
} public boolean isBreak(String s, Set<String> dict) {
boolean[] canBreak = new boolean[s.length()+1];
canBreak[0] = true; for(int i=1; i<=s.length(); i++) {
boolean flag = false;
for(int j=0; j<i; j++) {
if(canBreak[j] && dict.contains(s.substring(j,i))) {
flag = true;
break;
}
}
canBreak[i] = flag;
}
return canBreak[s.length()];
}
}
Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode的更多相关文章
- 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 ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 【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】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 Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
随机推荐
- MATLAB 中的randn函数
matlab函数 randn:产生正态分布的随机数或矩阵的函数 randn:产生均值为0,方差σ^2 = 1,标准差σ = 1的正态分布的随机数或矩阵的函数. 用法: Y = randn(n):返回一 ...
- connection timeout 和command timeout
每次对数据库连接时,我们有时候会碰到连接超时或者命令超时,这两个超时是不一样的.以ADO.NET为例,当客户端和服务器端连接时,碰到的超时情况主要有下面几种: ''' 当从连接池获取一个连接时,碰到超 ...
- AIX 10G HA RAC卸载
删除 1:crs_stat –t资源都停掉 2:停ha 3: 删除oracle 4:删除crs 5: 删除ha smit hacmp 6: 删除vg exportvg 7;卸载hacmp smitty
- js 类似于移动端购物车删除,左移动删除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WPF知识点--渐变色(LinearGradientBrush、GradientStop)
[LinearGradientBrush-- 使用线性渐变绘制区域](https://msdn.microsoft.com/zh-cn/library/system.windows.media.lin ...
- CE工具里自带的学习工具--第三关
图解: 重复第5,6,7,8,9步,最终得到:
- C#NumberFormatInfo类
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0sAAAD2CAIAAACImosXAAAgAElEQVR4nOy9V3Nk13X+vTt3owPSJJ ...
- MySQL-----备份(转储)
备份: **备份数据表结构+数据** mysqldump -u root 要备份的数据库表名 > 要备份的数据的备份名(这里也可以指定路径) -p **备份数据表结构** mysqldump - ...
- FreeRTOS--疑难杂症
花了3个晚上,把这个章节看完,受益匪浅. 最有用的应该是与中断相关的错误,优先排查中断优先级设置. 堆栈溢出检查,可能用到,一般先把堆栈设置的足够大,只要没有溢出就是好事,溢出了,掌握了栈溢出钩子函数 ...
- UVA 1596 Bug Hunt (大模拟 栈)
题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为 ...