#leetcode刷题之路30-串联所有单词的子串
给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入:
s = "barfoothefoobarman",
words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
输入:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
输出:[]
思路:首先为了方便比较,把原vector和用于存储的目标vector都要排序,排序后就可以知道两者是否完全相等了;剩下的就是暴力循环;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> findSubstring(string s, vector<string>& words) {
sort(words.begin(),words.end());
vector<int> ans;
vector<string> temp;
int n=words.size();
if(n==) return ans;
int len=words[].length();
if(s.length()<n*len) return ans;
for(int i=;i<s.length()-n*len+;i++)
{
int t=i;
if(find(words.begin(),words.end(),s.substr(t,len))!=words.end())
{
temp.push_back(s.substr(t,len));
t=t+len;
}
for(int j=;j<n;j++)
{
if(find(words.begin(),words.end(),s.substr(t,len))!=words.end())
{
temp.push_back(s.substr(t,len));
t=t+len;
}
else
{
temp.clear();
break;
}
}
sort(temp.begin(),temp.end());
if(temp==words) ans.push_back(i);
temp.clear();
}
return ans;
} int main() {
string s="aaa";
vector<string> a={"a","a"};
vector<int> ans=findSubstring(s,a);
std::cout << ans.size() << std::endl;
//std::cout << ans[0]<<ans[1] << std::endl;
return ;
}
#leetcode刷题之路30-串联所有单词的子串的更多相关文章
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(一)
LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...
- #leetcode刷题之路40-组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...
- #leetcode刷题之路38-报数
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下:1. 12. 113. 214. 12115. 1112211 被读作 "one 1" (&quo ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- #leetcode刷题之路13-罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...
随机推荐
- 新手,再来1个 vue2入门的教程,有源码参考
在这之前有入门的,作者写的不错的, 照着来一下,也收益颇多,上个例子是基于 "dependencies": { "vue": "^2.2.6&qu ...
- VS2008和VC6.0下使用pthread.h头文件
原文:http://www.cppblog.com/liquidx/archive/2009/06/16/87811.html 要在windows环境下使用 #include <pthread. ...
- Windows ->> Windows Server 2012打开管理添加“我的电脑”桌面图标途径
Windows Server 2012打开管理添加“我的电脑”桌面图标途径 rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0
- 关于VSTS自动Build报错问题之Microsoft.Net.Compilers
报错内容如下: --06T11::.6035712Z ##[error]Dotnet command failed with non-zero exit code on the following p ...
- 第六次作业——Excel制作工资表
- php测试工具
如果是测压力有apache的ab如果要看性能则有xdebug和xhprof.还有linux的strace命令来跟踪程序的执行时的系统调用
- Eclipse+Maven 项目创建
★:jar包下载不了的话可能是镜像里没有这个版本,换个低版本的就行 ★:eclipse工程validating很慢,可以先关掉验证(一般对项目没什么影响) ★:eclipse工程pom.xml文件报错 ...
- [EffectiveC++]item03:尽可能使用const 并且转载一篇关于const函数的博客
速度 #include <iostream> using namespace std; class TextBlock { private: string text; public: Te ...
- php截取后台登陆密码的代码
php截取后台登陆密码的代码,很多时候黑客留了这样的代码,大家一定要注意下if($_POST[loginsubmit]!=){ //判断是否点了登陆按钮 $sb=user:.$_POST[userna ...
- IOS XMPP(即时通讯的框架)
#import "AppDelegate.h" #import "XMPPFramework.h" /* * 在AppDelegate实现登录 1. 初始化XM ...