You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

vector<int> findSubstring(string S, vector<string> &L) {

    vector<int> result;
if ( S.size()<= || L.size() <= ){
return result;
} int n = S.size(), m = L.size(), l = L[].size(); //put all of words into a map
map<string, int> expected;
for(int i=; i<m; i++){
if (expected.find(L[i])!=expected.end()){
expected[L[i]]++;
}else{
expected[L[i]]=;
}
} for (int i=; i<l; i++){
map<string, int> actual;
int count = ; //total count
int winLeft = i;
for (int j=i; j<=n-l; j+=l){
string word = S.substr(j, l);
//if not found, then restart from j+1;
if (expected.find(word) == expected.end() ) {
actual.clear();
count=;
winLeft = j + l;
continue;
}
count++;
//count the number of "word"
if (actual.find(word) == actual.end() ) {
actual[word] = ;
}else{
actual[word]++;
}
// If there is more appearance of "word" than expected
if (actual[word] > expected[word]){
string tmp;
do {
tmp = S.substr( winLeft, l );
count--;
actual[tmp]--;
winLeft += l;
} while(tmp!=word);
} // if total count equals L's size, find one result
if ( count == m ){
result.push_back(winLeft);
string tmp = S.substr( winLeft, l );
actual[tmp]--;
winLeft += l;
count--;
} }
} return result;
}

用map容器。

i从0到L-1。

actual[word] > expected[word]时舍弃前面的单词,向后查找。

30. Substring with Concatenation of All Words *HARD*的更多相关文章

  1. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  2. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  3. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  4. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

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

  6. [LeetCode] 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 ...

  7. Java [leetcode 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 a ...

  8. 【LeetCode】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】#30. Substring with Concatenation of All Words

    注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...

  10. [leetcode]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 ...

随机推荐

  1. 20145317彭垚《网络对抗》Exp2 后门原理与实践

    20145317彭垚<网络对抗>Exp2 后门原理与实践 基础问题回答 例举你能想到的一个后门进入到你系统中的可能方式? 在网上下载软件的时候,后门很有可能被捆绑在下载的软件当中: 例举你 ...

  2. GD32芯片移植完全攻略

    GD32是国产兆易创新公司生产的完全兼容STM32系列的Cortex-M3处理器,具有几大亮点:1,高主频108MHz.性能提升30%以上,可超频到120MHz2,Flash零等待.STM32的72M ...

  3. python判断结构总结

    1.判断结构是允许程序针对不同情况执行不同指令序列的控制结构. 2.判断在Python中用if语句实现.简单的判断是用一个简单的if来实现的.两路判断通常使用if-else.多路判断用if-elif- ...

  4. JS(JavaScript)脚本库的积累

    在现在互联网盛行的时代,使得B/S架构飞速发展.曾经在大学的时候我一直都梦想着毕业后要找一个像腾讯这样大企业做C/S方面的开发工作(其实现在腾讯也有很多B/S软件),因为C/S体验度非常高,感觉非常好 ...

  5. Tempter of the Bone(dfs+奇偶剪枝)题解

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. fread

    快速读入? 不知道 反正只是贴过来的 以后或许会用到? #define FI(n) FastIO::read(n) namespace FastIO { << ; ]; int bi = ...

  7. Web前端代码规范

    新增:http://materliu.github.io/code-guide/#project-naming HTML 原则1.规范 .保证您的代码规范,保证结构表现行为相互分离.2.简洁.保证代码 ...

  8. Codeforces Round #323 (Div. 2) C. GCD Table map

    题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...

  9. spoj MINSUB 单调栈+二分

    题目链接:点击传送 MINSUB - Largest Submatrix no tags  You are given an matrix M (consisting of nonnegative i ...

  10. Could not find a package configuration file provided by "ecl_build",.................couldn't find required component 'ecl_build'

    sudo apt-get install ros-kinetic-ecl-build