先对words中的单词排列组合,然后对s滑窗操作;部分样例超时,代码如下:

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
//dfs找出words所有组合,然后在s滑窗 //排除异常情况
int len=0;
for(auto w:words){
len+=w.size();
}
if(len==0 || s.size()==0 || len>s.size()) return {}; //排列组合words
set<string> s_words;
vector<int> indexs; for(int i=0;i<words.size();i++){
indexs.push_back(i);
}
do{
string tmp="";
for(int i=0;i<indexs.size();i++){
tmp+=words[indexs[i]];
}
s_words.insert(tmp); }while(next_permutation(indexs.begin(),indexs.end())); //在s中滑窗寻找起始位置,O(m*n*k) m为s长度,n为s_words字符串个数,k为subs的长度;
vector<int> res;
for(int i=0;i<=s.length()-len;i++){
string subs=s.substr(i,len);
if(s_words.find(subs)!=s_words.end()){
res.push_back(i);
}
}
return res;
}
};

下面也超时,均执行至148/173

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
//dfs找出words所有组合,然后在s滑窗 //排除异常情况
int len=0;
for(auto w:words){
len+=w.size();
}
if(len==0 || s.size()==0 || len>s.size()) return {}; //排列组合words
set<string> s_words;
vector<int> indexs; for(int i=0;i<words.size();i++){
indexs.push_back(i);
}
do{
string tmp="";
for(int i=0;i<indexs.size();i++){
tmp+=words[indexs[i]];
}
s_words.insert(tmp); }while(next_permutation(indexs.begin(),indexs.end())); //在s中滑窗寻找起始位置,O(m*n*k) m为s长度,n为s_words字符串个数,k为subs的长度;
vector<int> res;
for(int i=0;i<=s.length()-len;i++){
string subs=s.substr(i,len);
for(auto it=s_words.begin();it!=s_words.end();it++){
string tmp=*it;
int flag=1;
for(int j=0;j<len;j++){
if(tmp[j]!=subs[j]){
flag=0;break;
}
}
if(flag==1) {
res.push_back(i);break;
}
}
}
return res;
}
};

想办法对暴力算法进行提速,

以下为别人的提速方案,可行;

作者:Xdo

链接:https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/solution/bao-li-suan-fa-jia-ru-qu-zhong-you-hua-10bei-ti-su/

思路就是,先把存在的字符串,放到 hashmap ,可以快速比较,然后每一个位置都进行匹配

但这里会有很多的重复计算,就可以使用一个小技巧,先计算目标串的每个字母的 ASCII 和,

然后和当前要匹配的字符串的每个字母的 ASCII 进行比较,如果不相等就不用进行下面的匹配过程了

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res;
if(words.size()<1 || s.size()<1 || s.size() < words[0].size()*words.size()) return res;
int wordLen = words[0].size(), lens = wordLen*words.size(), target = 0, cur = 0;
unordered_map<string,int> allWord;
for(auto& it:words){
allWord[it]++;
for(auto& i:it) target += i;
}
for(int i=0; i<lens; i++) cur += s[i];
// 先看当前字符串的 ASCII 码相加是否相等 方便去重
for(int i=0, j; i<=s.size()-lens; cur -= s[i], cur += s[lens + i++]){
// 快速去重
if(cur != target) continue;
// 确认一下,是否为真的匹配
unordered_map<string,int> tem(allWord);
for(j=i; j<i+lens; j+=wordLen)
if(tem[s.substr(j, wordLen)]-- == 0) break;
if(j == i+lens) res.push_back(i);
}
return res;
}
};

leetcode30 串联所有单词的子串的更多相关文章

  1. [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  2. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

    [030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...

  3. [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

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

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

  5. [LeetCode] 30. 串联所有单词的子串

    题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...

  6. Leetcode 30 串联所有单词的子串 滑动窗口+map

    见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...

  7. 【LeetCode 30】串联所有单词的子串

    题目链接 [题解] 开个字典树记录下所有的单词. 然后注意题目的已知条件 每个单词的长度都是一样的. 这就说明不会出现某个字符串是另外一个字符串的前缀的情况(除非相同). 所以可以贪心地匹配(遇到什么 ...

  8. [Swift]LeetCode30. 与所有单词相关联的字串 | Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. #leetcode刷题之路30-串联所有单词的子串

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

随机推荐

  1. Redis和MemCache静态Map做缓存区别

    本地缓存和分布式缓存 本地缓存:使用自带的map或者guava实现的是本地缓存,最主要的特点是轻量以及快速,生命周期随着jvm的销毁而结束,并且在多实例的情况下,每个实例都需要各自保存一份缓存,缓存不 ...

  2. VM安装vmtools后centos7无法上网

    先安装VmTools工具 文件 /etc/sysconfig/network-scripts/ifcfg-ens33(这里的enp0s3不是固定的,看你具体情况,但是基本是en开头的) 将 ONBOO ...

  3. ARMA(p,q)模型数据的产生

    一.功能 产生自回归滑动平均模型\(ARMA(p,q)\)的数据. 二.方法简介 自回归滑动平均模型\(ARMA(p,q)\)为 \[ x(n)+\sum_{i=1}^{p}a_{i}x(n-i)=\ ...

  4. Image Processing and Computer Vision_Review:A survey of recent advances in visual feature detection—2014.08

    翻译 一项关于视觉特征检测的最新进展概述——http://tongtianta.site/paper/56761 摘要 -特征检测是计算机视觉和图像处理中的基础和重要问题.这是一个低级处理步骤,它是基 ...

  5. 程序员和IT员不能错过的网站

    前言本文收录一些值得收藏的开发相关网站. 目录一.搜索引擎二.在线课程三.在线练习四.在线工具箱五.在线编译器六.技术论坛或社区七.音乐放松一下 一.搜索引擎搜索引擎大家最熟悉不过,本没有必要列出,但 ...

  6. dropbear源码编译安装及AIDE软件监控

    ssh协议的另一个实现:dropbear源码编译安装:• 1.安装开发包组:yum groupinstall “Development tools”• 2.下载 -2017.75.tar.bz2    ...

  7. 一周死磕fastreport ----ASP.NET (二)

    前一章忘了为什么要死磕fastreport  了,这次简单说一下,  公司本来有一个winfrom  窗体打印程序,可是上司觉得太麻烦了,(前几天 我一直在做web版看板,然后发现还不错,于是 想把公 ...

  8. Java中各种对象(PO,BO,VO,DTO,POJO,DAO,Entity,JavaBean,JavaBeans)的区分

    PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架中的Entity,po的每个属性基本上都对应数 ...

  9. 算法---FaceNet理论学习篇

    FaceNet算法-理论学习篇 @WP20190228 ==============目 录============ 一.LFW数据集简介 二.FaceNet算法简介 FaceNet算法=MTCNN模型 ...

  10. CDQ 分治解决和点对有关的问题

    具体可以去这篇博客学习: https://oi-wiki.org/misc/cdq-divide/