140. 单词拆分 II
Q:
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。
说明:
分隔时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:
输入:
s = “catsanddog”
wordDict = [“cat”, “cats”, “and”, “sand”, “dog”]
输出:
[
“cats and dog”,
“cat sand dog”
]
示例 2:
输入:
s = “pineapplepenapple”
wordDict = [“apple”, “pen”, “applepen”, “pine”, “pineapple”]
输出:
[
“pine apple pen apple”,
“pineapple pen apple”,
“pine applepen apple”
]
解释: 注意你可以重复使用字典中的单词。
示例 3:
输入:
s = “catsandog”
wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
输出:
[]
A:
关键是要先用139的代码算一下给的字符串能不能被拆分为字典里的单词,不能直接返回。因为算是否能被拆分是O(N ^ 2),而本题要求的所有序列是O(N ^ 3),不先判断一下有一个全是’a’的用例是过不去的。。
C++:
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
vector<vector<string>> dp(s.size(),vector<string>());
unordered_set<string> dic;
for(auto& str:wordDict){
dic.insert(str);
}
vector<bool> judge(s.size(),false);
for(int i=0;i<s.size();++i){
if(dic.count(s.substr(0,i+1))){judge[i]=true;}
else{
for(int j=0;j<=i;++j){
if(judge[j] and dic.count(s.substr(j+1,i-j))){
judge[i]=true;
break;
}
}
}
}
if(not judge.back()){return {};}
//dp[i]表示s截止到i的可能分割出的句子
for(int i=0;i<s.size();++i){
for(int j=0;j<=i;++j){
if(dic.count(s.substr(j,i-j+1))){
string str2=s.substr(j,i-j+1);
if(j==0){
dp[i].push_back(str2);
continue;
}
if(not judge[j-1]){continue;}
for(auto& str1:dp[j-1]){
dp[i].push_back(str1+" "+str2);
}
}
}
}
return dp[s.size()-1];
}
};
python:
class Solution:
def wordBreak(self, s: str, WordDict):
l=len(s)
if not l:
return 0
dic={ x:1 for x in WordDict}
#上一题代码拿来用
judge=[0 for i in range(l)]
judge[0]=int(s[0] in dic) #i==0递推基础
for i in range(1,l): #i==1开始遍历
if s[:i+1] in dic:
judge[i]=1
else:
for j in range(i):
if judge[j] and s[j+1:i+1] in dic:
judge[i]=1
break
print(judge)
if not judge[-1]:
return []
#开始算本题所求的序列
dp=[[]for i in range(l)] #dp[i]是一个列表,存储不同的s[1,i]闭区间的有效拆分序列
if s[0] in dic:
dp[0].append(s[0]) #递推起始状态
for i in range(1,l): #从i==1开始自底向上递推
if s[:i+1] in dic: #s[0:i+1]本身就在字典里则加入解集
dp[i].append(s[:i+1])
for cut in range(i): #进行切分,符合条件的加入解集
if s[cut+1:i+1] in dic and judge[cut]:
#cut前面的子问题有解且cut之后到i的字符串在字典中
for x in dp[cut]: #x为字符串
dp[i].append(x+' '+s[cut+1:i+1])
#把dp[cut]中的每个解加上cut后的字符串,插入dp[i]的解集
# print(dp)
return dp[-1]
140. 单词拆分 II的更多相关文章
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
- [LeetCode] 140. 单词拆分 II
题目链接 : https://leetcode-cn.com/problems/word-break-ii/ 题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符 ...
- Java实现 LeetCode 140 单词拆分II
class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- leetcode 140 单词拆分2 word break II
单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...
- 算法——单词拆分 II
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 链接: leetcode. 解题思路 ...
- [Swift]LeetCode140. 单词拆分 II | Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
随机推荐
- linux基础之IO模型
一.IO模型 一次read操作: (1)等待数据准备好:从磁盘到内核内存 (2)从内核内存复制到进程内存 示意图如下: I/O类型: 同步和异步:synchronous,asynchronous 关注 ...
- 安装AB到CentOS(YUM)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:httpd-tools-2.4.6 硬件要求:无 安装过程 1.安装YUM-EPEL源 HTTP ...
- Linux shell unzip和tar 批量解压文件
#!/bin/sh UNTAR="tar -xvf " #unzip all zip files function unzip_all_file() { for i in *.zi ...
- archlinux install.txt
++++++ 注意事项+++ +++++++++++++++++++++++++++ 强烈建议新手移步 Arch Wiki > 新手指南 经验者请参阅 Arch Wiki > 安装指南 若 ...
- Common Subsequence POJ - 1458 最长公共子序列 线性DP
#include <iostream> #include <algorithm> #include <string> #include <cstring> ...
- H3C虚拟化技术
一.IRF简单介绍 IRF(Intelligent Resilient Framework,智能弹性架构)是H3C自主研发的软件虚拟化技术.它的核心思想是将多台设备连接在一起,进行必要的配置后,虚拟化 ...
- zabbix的web界面出现乱码解决方案
1.问题描述:当我们搭建好zabbix服务器后,查看监控信息时,发现数据显示的下端文字显示为乱码. 2.解决方式: (1)拷贝windows系统字体: 可根据各自的喜好进行选择,我这边就选择楷体 常规 ...
- Java判断一个字符串是否有中文
Java判断一个字符串是否有中文一般情况是利用Unicode编码(CJK统一汉字的编码区间:0x4e00–0x9fbb)的正则来做判断,但是其实这个区间来判断中文不是非常精确,因为有些中文的标点符号比 ...
- Linux常用命令: zip、unzip 压缩和解压缩命令
zip基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] 常用参数: -a 将文件转成ASCII模式 -F 尝试修复损坏的压缩文件 -h 显示帮助界面 -m 将文件压缩之后,删除源 ...
- linux 安装 Django14
一.实际安装过程(只操作这一步就可以安装) ## 下载并安装 rpm -ivh http://cbs.centos.org/kojifiles/packages/Django14/1.4.20/1.e ...