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 ...
随机推荐
- CentOS6.8 RPM包安装快速zabbix22
CentOS6.8 RPM包安装快速zabbix22 yum install -y epel-release # yum install -y httpd php php-devel mysql-se ...
- 自定义对话框(jDialog)
[配置项]jDialog options点击收起 一.接口功能 jDialog的默认配置项,本组件提供的所有对话框,都可以通过修改这些配置项来实现不同的效果. 二.详细配置项 /** * 对话框的默认 ...
- leetcode_998. Maximum Binary Tree II
https://leetcode.com/problems/maximum-binary-tree-ii/ 在654. Maximum Binary Tree版本的建树基础上,在最后插入一个数. 新节 ...
- mybatis generator 覆盖xml文件
mybatis generator默认采用追加方式生成,所以我们如果要重新生成代码的时候那么要先删除原来的文件. 解决办法: 1:创建一个自定义补丁类. OverwriteXmlPlugin.java ...
- Jupyter IPython dead kernel and do not restart
本人遇到的情况:dead kernel & try to restart failed 查看CMD发现这个库安装有问题 解决办法 1.pip uninstall backports.shuti ...
- Android之多种Bitmap效果(4)
1. 将图片变为圆角 2. 获取缩略图图片 3. LOMO特效 4. 旧时光特效 5. 暖意特效 6. 根据饱和度.色相.亮度调整图片 7. 添加图片外边框 8. 添加内边框 9. 创建一个缩放的图片 ...
- 大项目之网上书城(六)——个人页面和书页面Demo
目录 大项目之网上书城(六)--个人页面和书页面Demo 主要改动 1.user.jsp 代码 效果图 user.js 代码 3.shu.jsp 代码 效果图 4.其他小改动 LoginServlet ...
- <Redis> 入门四 Jedis操作Redis
pom依赖 <dependencies> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> < ...
- [Python3网络爬虫开发实战] 1.8.2-Scrapy的安装
Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...
- ubuntu 虚拟机系统调优
Ubuntu虚拟机镜像最佳实践 分区/boot >1G/root >10G/var >5G配swap空间,内存的2倍 vi /etc/secur ...