给定一个字符串 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-串联所有单词的子串的更多相关文章

  1. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

  2. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  3. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  5. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  6. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  7. #leetcode刷题之路38-报数

    报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下:1. 12. 113. 214. 12115. 1112211 被读作 "one 1" (&quo ...

  8. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  9. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

随机推荐

  1. The String class's judging function

    字符串的判断功能: package com.itheima_03; /* * Object:是类层次结构中的根类,所有的类都直接或者间接的继承自该类. * 如果一个方法的形式参数是Object,那么这 ...

  2. AutoMapper6扩展

    简介 很多时候我们使用AutoMapper的时候,都需要进行一个配置才可以使用Mapper.Map<Source,Target>(entity);.如果不进行配置则会报错. 如果实体过多, ...

  3. 《深入理解mybatis原理》 Mybatis数据源与连接池

    对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构进行详尽的分析,并且深入解析MyBatis的连接池. 本文首先会讲述MyB ...

  4. Linux NFS 详解

    目录:      1.了解NFS服务      2.NFS主要文件      3.NFS安装      4.NFS配置     5.NFS客户端配置 6.固定NFS端口      7.Windows挂 ...

  5. 删除 maven仓库,更新失败的jar包命令

    set REPOSITORY_PATH=D:\maven_cpbsrem ص‎شعثرث÷...for /f "delims=" %%i in ('dir /b /s " ...

  6. July 28th 2017 Week 30th Friday

    If equal affection cannot be, let the more loving be me. 如果没有相等的爱,那就让我爱多一点吧. There is seldom equal a ...

  7. python网络爬虫--简单爬取糗事百科

    刚开始学习python爬虫,写了一个简单python程序爬取糗事百科. 具体步骤是这样的:首先查看糗事百科的url:http://www.qiushibaike.com/8hr/page/2/?s=4 ...

  8. GPU性能:光栅化、图层混合、离屏渲染

    So, shouldRasterize will not affect the green/red you see using Instruments. In order to have everyt ...

  9. [WebKit] JavaScriptCore解析--基础篇 (一)JSC与WebCore

    先看一下官方的基本介绍,短短几句就塞满了关键字. SquirrelFish,正式名称是JavaScriptCore,包括register-based(基于寄存器的虚拟机), direct-thread ...

  10. yii2.0 联表查询数据库报错:undefined index order_id

    1.在查询时加了->select();如下,要加上order_id,即关联的字段(比如:order_id)比如要在select中,否则会报错:undefined index order_id / ...