word-break-ii leetcode C++
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"].
C++
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict){
vector<string> result;
if(dict.find(s)!=dict.end())
result.push_back(s);
for(int i=1;i<s.size();i++) {
string w = s.substr(i);
if(dict.find(w) == dict.end())
continue;
string str = s.substr(0,i);
vector<string> left = wordBreak(str,dict);
Add(left,w);
result.insert(result.begin(), left.begin(), left.end());
}
return result;
}
void Add(vector<string> &str, string w){
for(vector<string>::iterator it=str.begin();it!=str.end();it++)
*it += " " + w;
}
vector<string> wordBreak2(string s, unordered_set<string> &dict){
vector<string> result;
if (dict.find(s) != dict.end())
result.push_back(s);
for(int i = s.size() -1; i > 0;i--){
string w = s.substr(i);
if(dict.find(w) == dict.end())
continue;
string str = s.substr(0,i);
vector<string> left = wordBreak(str,dict);
Add(left,w);
result.insert(result.end(),left.begin(),left.end());
}
return result;
}
};
word-break-ii leetcode C++的更多相关文章
- Word Break II leetcode java
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- Word Break II——LeetCode
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Word Break II -- leetcode
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: 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 ...
- 【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 ...
- 【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 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 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 ...
随机推荐
- nmap使用命令(转载)原文地址https://www.jianshu.com/p/4030c99fcaee
- 【Azure API 管理】APIM 配置Validate-JWT策略,验证RS256非对称(公钥/私钥)加密的Token
问题描述 在APIM中配置对传入的Token进行预验证,确保传入后端被保护的API的Authorization信息正确有效,可以使用validate-jwt策略.validate-jwt 策略强制要求 ...
- Django学习day03随堂笔记
每日测验 """ 今日考题 1.什么是静态文件,django静态文件配置如何配置,如何解决接口前缀不断变化,html页面上路径的引用需要反复修改的问题 2.request ...
- Hadoop-3.1.3安装
0.创建用户并付权限 sudo useradd iwbdsudo passwd iwbd 配置iwbd用户具有root权限 修改/etc/sudoers文件,找到下面一行(91行),在root下面添加 ...
- 网络协议之:WebSocket的消息格式
目录 简介 WebSocket的握手流程 webSocket的消息格式 Extensions和Subprotocols 总结 简介 我们知道WebSocket是建立在TCP协议基础上的一种网络协议,用 ...
- android web外壳
参考: 1.https://blog.csdn.net/m0_37201243/article/details/106862817 2.https://www.cnblogs.com/ifaswind ...
- [转载]Windows 2008多用户同时远程登陆配置方法
有些朋友需要在在使用Windows 2008远程登录功能时,进行多用户登录,那么就可以采用以下配置方法: 首先要启用远程桌面这一功能:右击"我的电脑"→ 属性 → 远程配置 → 远 ...
- Kafka 性能篇:为何 Kafka 这么快?
『码哥』的 Redis 系列文章有一篇讲透了 Redis 的性能优化 --<Redis 核心篇:唯快不破的秘密>.深入地从 IO.线程.数据结构.编码等方面剖析了 Redis " ...
- 深入理解Python切片
Python序列的切片很基础同时也很重要,最近看到一个[::-1]的表达,不明所以,查了一些资料并实际操作,对Python切片有了更深刻的认识,以下结合例子详细说明.先看下切片的基本语法,一般认为切片 ...
- 鸿蒙内核源码分析(调度机制篇) | 任务是如何被调度执行的 | 百篇博客分析OpenHarmony源码 | v7.07
百篇博客系列篇.本篇为: v07.xx 鸿蒙内核源码分析(调度机制篇) | 任务是如何被调度执行的 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调 ...