[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 ...
随机推荐
- How Many Trees?[HDU1130]
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Sky数[HDU2097]
Sky数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- BZOJ3495 : PA2010 Riddle
2-SAT. 建立n个变量,其中第i个变量表示第i个城市是否是首都. 对于边(x,y),连边x->y',y->x'. 对于一个有y个城市的国家,新建2y个变量,分别表示前i个城市和后i个城 ...
- Android MVVM框架RoboBinding初探
RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架.MVVM模式是MVC模式的重要更新,使得项目结构更加的优美,易于维护以及方便 ...
- IOS 获取最新设备型号方法
1.IOS 获取最新设备型号方法列表最新对照表:http://theiphonewiki.com/wiki/Models方法: #import "sys/utsname.h” struct ...
- TYVJ P1047 乘积最大 Label:dp
背景 NOIP 2000 普及组 第三道 描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力 ...
- POJ 1625 Censored!(大数+DP)
题目链接 这题,真心木啥意思,就是数据里貌似字符有负数,注意gets读入.. #include <iostream> #include <cstring> #include & ...
- Sublime Text 2的快速入门和常用技巧
1. 安装扩展器包管理器Package Control组件 点击菜单 View -> Show Console 调出控制台或者按快捷键 “Ctrl + `”(1左边的符号,可能和QQ拼音输入法和 ...
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) ERROR 2013 (HY00 ...
- Ajax注册验证用户名是否存在 ——引自百度经验
Ajax注册验证用户名是否存在 http://jingyan.baidu.com/article/a948d6515fdf870a2dcd2e85.html