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. 小K(wifi)插座剖解

    1.主控 AR9331 400MHZ MIPS 24k内核 2.flash:w9425G6JH-5 1352P 6316CF500ZY  RAM 32M

  2. FTP-FileZilla

    服务器上安装FileZilla Server连接时报You appear to be behind a NAT router. Please configure the passive mode se ...

  3. <OFFER03>03_01_DuplicationInArray

    #include<cstdio> bool duplicate(int numbers[], int length, int* duplication) { ) return false; ...

  4. FJUT Home_W的gcd(乱搞)题解

    题意: 给出一个序列a1,a2,a3,……an. HOME_W想在其中挖掘二元组,其中二元组的挖掘方法如下. 对于任意整数 l,r ,可得到一个二元组(l,gcd(al,al+1,……,ar)). H ...

  5. POJ 2823 Sliding Window(单调队列 || 线段树)题解

    题意:求每个长度为k的数组的最大值和最小值 思路: 1.用线段树创建维护最大值和最小值,遍历询问,简单复习了一下...有点手生 2.单调队列: 可以看一下详解 单调队列顾名思义就是一个单调递增或者递减 ...

  6. 【核心API开发】Spark入门教程[3]

    本教程源于2016年3月出版书籍<Spark原理.机制及应用> ,在此以知识共享为初衷公开部分内容,如有兴趣,请支持正版书籍. Spark综合了前人分布式数据处理架构和语言的优缺点,使用简 ...

  7. poj 2449 Remmarguts' Date 求第k短路 Astar算法

    =.=好菜 #include <iostream> #include <cstdio> #include <string.h> #include <cstri ...

  8. FPGA 概述

    概述 verilog HDL Verilog HDL基本结构 1 Verilog HDL程序是由模块构成的.每个模块嵌套在module和endmodule声明语句中. 2 每个Verilog HDL源 ...

  9. UVa 1626 括号序列(矩阵连乘)

    https://vjudge.net/problem/UVA-1626 题意: 输入一个由 "(" . ")" . "[" . " ...

  10. python自动制作gif并添加文字

    引言     最近租的房子快到期了,哎,因为去年是第一次找房子租,结果遇到了一个东北黑中介,押一付三,房子有啥问题,灯坏了,下水道堵了,原来签合同的时候说的客气,说是马上就会上门解决,结果实际上我每次 ...