[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 ...
随机推荐
- 详解Adorner Layer(zz)
首先,千万不要觉得Adorner离你很远,因为最简单的WPF界面也会用到Adorner.在WPF中,下面的几个很常见的功能,都是用Adorner实现的. 1. 光标(caret) 2. ...
- python 代码片段11
#coding=utf-8 #python里面的字典,用{}来表示 book={'title':'python web development','year':2008} print book pri ...
- unity scroolView 和 groupview
#pragma strict var scrollPosition:Vector2; function Start () { scrollPosition[0]=50; scrollPosition[ ...
- 【wikioi】1913 数字梯形问题(费用流)
http://wikioi.com/problem/1913/ 如果本题没有询问2和3,那么本题和蚯蚓那题一模一样.http://www.cnblogs.com/iwtwiioi/p/3935039. ...
- lightning mdb 源代码分析(4)—MVCC/COW
本博文将描述MVCC和cow技术以及LMDB中如何使用以及实现这两种技术. COW(Copy On Write): COW技术背后的思想是拖延技术,基本方法是假如有多个调用者需要访问的资源,在其初始化 ...
- CSS3 Media Queries在iPhone4和iPad上的运用
CSS3 Media Queries的介绍在W3CPlus上的介绍已有好几篇文章了,但自己碰到的问题与解决的文章还是相对的较少.前几天在<修复iPhone上submit按钮bug>上介绍了 ...
- 什么是java 键值对
所谓键值对,你可以查看jdk文档,找MAP接口,它的实现类都是键值对的形式保存数据的 键:就是你存的值的编号值:就是你要存放的数据
- 在CSV文件中增加一列属性值
具体参见:系统管理\将文件夹复制到列表中的远程主机 修改前: column1, column2 1,b 2,c 3,5 修改后: column1, column2, column3 1,b, ...
- 使用Grunt启动和运行
开始使用Grunt 大多数开发人员都一致认为,JavaScript开发的速度和节奏在过去的几年里已经相当惊人.不管是Backbone.js和Ember.js的框架还是JS Bin社区,这种语言的发展变 ...
- mysql 导入导出.sql文件
备份数据库(包含全部表和全部存储过程):C:\Documents and Settings\Administrator>mysqldump -h localhost -u root -p -R ...