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)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. [leetcode]Substring with Concatenation of All Words @ Python

    原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 题意: You are given a ...

  5. 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 ...

  6. Leetcode:Substring with Concatenation of All Words分析和实现

    题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...

  7. LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!

    超时代码 class Solution { public: vector<int> findSubstring(string s, vector<string>& wo ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  2. POJ 3020 (二分图+最小路径覆盖)

    题目链接:http://poj.org/problem?id=3020 题目大意:读入一张地图.其中地图中圈圈代表可以布置卫星的空地.*号代表要覆盖的建筑物.一个卫星的覆盖范围是其周围上下左右四个点. ...

  3. CentOS6.4 内核优化

    vi /etc/sysctl.conf net.ipv4.tcp_syncookies = net.ipv4.tcp_tw_reuse = net.ipv4.tcp_tw_recycle = net. ...

  4. CentOS6.4 利用sendEmail发邮件

    1.下载安装sendEmail wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz tar z ...

  5. 封装同步的UIActionSheet

    封装同步的UIActionSheet 发问题 做 iOS 开发的同学想必都用过 UIActionSheet.UIActionSheet 可以弹出一个选择列表,让用户选择列表中的某一项操作.使用 UIA ...

  6. hdu Proud Merchants

    此题是一个背包的题目,刚开始我并没有作任何的排序工作,所以出来的结果总是错的,仔细想想也确实是不对的,因为q[i]会限制dp[i]的值的变化.虽然我知道要按照某个量进行排序,对原数据进行处理,但是实在 ...

  7. FreeBSD Intel SYSRET Kernel Privilege Escalation Exploit

    /* * FreeBSD 9.0 Intel SYSRET Kernel Privilege Escalation exploit * Author by CurcolHekerLink * * Th ...

  8. Servlet 编程 请求的转发

    在上篇的基础上,修改servlet *转发只能在同一应用内转发. 将forward 地址改为:youku.com  不能访问 重定向是可以访问外部应用的

  9. 在树莓派上使用ss和iptables实现fq功能

    VPS购买地址 以下所有叙述均来自互联网上已有文章, 本人只做收集和整理工作. 写在前面的话: 一直想把家里的树梅派做成一个fq路由器, 期间也看过很多GitHub上的开源项目: Redsock, C ...

  10. php常用Stream函数集介绍

    php常用Stream函数集介绍 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-24   本篇文章是对php中的常用Stream函数集进行了详细的分析介绍,需要的朋友参考下     ...