#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 写 ...
随机推荐
- Leetcode算法比赛---- Lexicographical Numbers
问题描述 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10 ...
- Google APAC----Africa 2010, Qualification Round(Problem B. Reverse Words)----Perl 解法
原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p1 问题描述: Problem Given a list of s ...
- 微信小程序-03-小程序开发框架
微信小程序-03-小程序开发框架 官方文档: https://developers.weixin.qq.com/miniprogram/dev/framework/MINA.html 小程序开发框架 ...
- Android链接蓝牙电子称
蓝牙一直是我内心屏蔽的一个模块哈哈哈哈!然而今天我不得不正视它了,我百度了看了好多因为需要设备匹配所以设备不在没办法测试,几天之后设备到了.因为没有接触过,看到返回的打印出来的菱形方块就以为是错了.于 ...
- ubuntu下面解压tar.gz包报错:tar命令报错: gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error
原因: 压缩包文件不完整(损坏或者其他原因) 比如今天下载的tomcat8,使用ubuntu的命令下载的 curl -O http://apache.mirrors.ionfish.org/tomca ...
- Java学习-1
数组 运算符 包 访问权限 修饰符 数组 1. 数组的声明: int[] a; 2. 数组的创建 使用new运算符数组的创建int[] a = new int[100] 数组的长度不要求是常量:new ...
- 【Oozie】ambari安装oozie失败
之前对azkaban的研究比较多,现在开个新坑,对Oozie开始深入了解 Traceback (most recent call last): File "/var/lib/ambari-a ...
- Gdb调试工具/ Makfile项目管理
gdb调试工具 gcc -g main.c -o main 常用命令 命令 简写 作用 help h 按模块列 ...
- c++的路上,我坚信,我可以 -----第四次作业体会
第四次作业 传送门 1.浅谈"新对象"sstream和stack 第四次作业,就是在第三次作业上作修改,上周周末,我刚刚才完成了第三次作业,但是知道了队列如何应用,面对这次的sta ...
- Server Host Cannot be null解决方法
在用打开Services Directory application 或者访问 某个已发布的地图服务时,出现"Server Host Cannot be null"的错误. 问题的 ...