[LeetCode] Substring with Concatenation of All Words(good)
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given: S: "barfoothefoobarman" L: ["foo", "bar"]
You should return the indices: [0,9]. (order does not matter).
方法:分组循环,不要在S中直接遍历,而是把S按照L中每个字符串的长度进行分组,妙
class Solution {
private:
vector<int> res;
map<string,int> cntL;
map<string,int> cn;//存储L中string及其出现次数
int n ;
public:
vector<int> findSubstring(string S, vector<string> &L){
res.clear();
cntL.clear();
cn.clear();
n = S.length();
int e = L.size();
int t = L[].length();
int k = ;//k表示L中一共有几个string
for(int i = ; i < e ; i++){//在cn中存储L中string及其出现次数
if(cn.count(L[i]) == ){
cn[L[i]] = ;
k++;
}else{
cn[L[i]] += ;
k++;
}
}//end for
string s0 ,s1;
int r = ;
int st = ;
for(int j = ; j < t ; j++){//L中每个string的长度是t
r = ; st = j;
cntL.clear();
for(int i = j; i < n; i += t){
s0 = S.substr(i,t);
if( cn.count(s0) == || cn[s0] == ){
cntL.clear();
r = ;
st = i+t;
}else if(cntL[s0] < cn[s0]){
cntL[s0] += ;//cntL中记录S中出现L中string及次数
r++;//r表示S中遇到的L中string的总共数 r <= k
}else{//如果S中子字符串比L中某个多了,则开始下标st必然要越过这个多出来的字符,这个多出来的字符即是s0
s1 = S.substr(st,t);
while(s1 != s0){
cntL[s1]--;
r--;
st += t;
s1 = S.substr(st,t);
}
st += t;
}
if(r == k){//利用上个记录,以免多用时间
res.push_back(st);
s1 = S.substr(st,t);
cntL[s1]--;
r--;
st += t;
}
}//end for
}//end for
sort(res.begin(),res.end());
return res ;
}//end func
};
[LeetCode] Substring with Concatenation of All Words(good)的更多相关文章
- LeetCode: Substring with Concatenation of All Words 解题报告
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- [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 ...
- LeetCode:Substring with Concatenation of All Words (summarize)
题目链接 You are given a string, S, and a list of words, L, that are all of the same length. Find all st ...
- [leetcode]Substring with Concatenation of All Words @ Python
原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 题意: You are given a ...
- Leetcode Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Leetcode:Substring with Concatenation of All Words分析和实现
题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...
- LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!
超时代码 class Solution { public: vector<int> findSubstring(string s, vector<string>& wo ...
- LeetCode HashTable 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 ...
- leetcode面试准备: Substring with Concatenation of All Words
leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...
随机推荐
- Fengshui-[SZU_B40]
Description Fengshui is an ancient subject in Chinese tradition. Someone considers it as science and ...
- BZOJ3322 : [Scoi2013]摩托车交易
求出最大生成树,则两点间的最大容量为树上两点间的边权的最小值. 设$lim[i]$表示第$i$个订单的城市允许携带的黄金上限,则 $lim[i]=\min(lim[i+1],a[i]和a[i+1]点间 ...
- BZOJ2707 : [SDOI2012]走迷宫
首先求出SCC缩点,E[T]=0,按拓扑序计算 对于无边连出的块,如果不是T所在块,则称该块是死路块 对于一个块,如果其中的点连出的边是死路块,则它也是死路块 否则对于每块进行高斯消元求出期望 如果S ...
- robotium 新建 android 测试项目:
注意:新建项目后再运行前一定要修改Manifest文件中的instrumentation 中的target package, 这个是测试的入口 1. 程序开始要通知系统我要测的app是什么 如何知道a ...
- [Unity2D]Box Collider 2D盒子碰撞器
盒子碰撞器(BoxCollider2D)是Unity2D中常用的碰撞器,所有为碰撞器,顾名思义,就是用于检测物体之间的碰撞情况的,Unity2D里面除了BoxCollider2D碰撞器之外还集成Box ...
- Idea_idea代码调试debug篇
主要看图,看图一目了然. 断点的设定和eclipse一样,只要点一下就可以,下面是我设定的几个断点,再下面的三个窗口是用来调试代码的,这个和eclipse类似 调试常用的快捷键 ? 1 2 3 4 5 ...
- Sea.js创始人玉伯的前端开发之路
在Web应用程序的用户体验越来越被重视的今天,前端开发的地位也上升到了前所未有的高度,而随之而来的也有更多的挑战. 为了将前端开发者繁重的工作变得简单,框架应运而生.国内也不乏一些非常优秀的前端开发框 ...
- (译)V8引擎介绍
V8是什么? V8是谷歌在德国研发中心开发的一个JavaScript引擎.开源并且用C++实现.可以用于运行于客户端和服务端的Javascript程序. V8设计的初衷是为了提高浏览器上JavaScr ...
- float塌陷有关问题
程序代码需要用到的CSS样式body{ margin:0px; padding:0px; text-align:center; font:Arial, Helvetica, sans-serif; f ...
- nodejs 更新最新版本
sudo npm cache clean -f sudo npm install -g n sudo n stable