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. POJ2186 Popular Cows(强连通分量)

    题目问一个有向图所有点都能达到的点有几个. 先把图的强连通分量缩点,形成一个DAG,那么DAG“尾巴”(出度0的点)所表示的强连通分量就是解,因为前面的部分都能到达尾巴,但如果有多个尾巴那解就是0了, ...

  2. 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  3. cocos2d 创建精灵图

    // 在init这个函数当中做一些初始化的事情 bool HelloWorld::init() { ////////////////////////////// // 先构造父级对象 if ( !CC ...

  4. BZOJ3807 : Neerc2011 Lanes

    左右与右左是两个独立的问题 设f[i]表示i时刻左右车道减少一条的答案 g[i]表示i时刻右左车道增加一条的答案 ans=min(f[i]+g[i+r]) 计算f[i]: 首先暴力计算出f[m+1], ...

  5. BZOJ3745 : [Coci2014]Norma

    考虑枚举右端点,用线段树维护[i,nowr]的答案. 当右端点向右延伸时,需要知道它前面第一个比它大/小的数的位置,这里面的最值将发生改变,这个使用单调队列求出,然后将所有的l都加1. 注意常数优化. ...

  6. Codeforces Round #191 (Div. 2) E题

    状态压缩DP,算sum,本来是枚举的,结果TLE了.. #include <iostream> #include <cstring> #include <cstdio&g ...

  7. edtftpj让Java上传FTP文件支持断点续传

    在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一 ...

  8. iOS-TextField知多少

    iOS-TextField知多少 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRect ...

  9. float塌陷有关问题

    程序代码需要用到的CSS样式body{ margin:0px; padding:0px; text-align:center; font:Arial, Helvetica, sans-serif; f ...

  10. 注意自己的dns设置 - 阿权的书房

    一般而言,随便找个合适的dns服务器作为自己的dns解析服务器即可,但如果选择不当,可能就会导致网络选择并不是最优的.这个情况一般发生在电信网通优化的域名上. 检查方法(域名有所替换): [root@ ...