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. MySQL备份与恢复-innobackupex

    :上一片myloder搞崩溃,为什么百度的博文都是抄袭一模一样的,哎烦! 这一片文章我们来介绍物理备份工具xtracebackup! 首先是安装可以percona官网下载安装,下载rpm包直接yum安 ...

  2. MySQL索引类型总结和使用技巧

    引用地址:http://www.jb51.net/article/49346.htm 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytable表: 复制代码 代码如下: C ...

  3. Python Web学习笔记之Python多线程和多进程、协程入门

    进程和线程究竟是什么?如何使用进程和线程?什么场景下需要使用进程和线程?协程又是什么?协程和线程的关系和区别有哪些? 程序切换-CPU时间的分配 首先,我们的任何一个程序都需要运行在一个操作系统中,如 ...

  4. c++的class声明及相比java的更合理之处

    或许是基于一直以来c/c++头文件声明和cXX实现物理上置于独立文件的考虑,c++中的OO在现实中基本上也是按照声明和实现分离的方式进行管理和编译,如下所示: Base.h #pragma once ...

  5. c++性能之map实现性能比较

    http://www.cnblogs.com/zhjh256/p/6346501.html讲述了基本的map操作,在测试的时候,发现map的性能极为低下,与java相比相差了接近200倍.测试的逻辑如 ...

  6. [Linux 003]——用户和用户组以及 Linux 权限管理(一)

    嗬!没想到吧!学习 Linux 的第三天,我们已经开始接触用户管理,用户组管理,以及权限管理这几个逼格满满的关键字.这几个关键字对于前端程序猿的我来说真的是很高大上有木有,以前尝试学 Linux 的时 ...

  7. Django组件(四) Django之Auth模块

    Auth模块概述 Auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能 ...

  8. linux下sz rz的正确用法

    一.背景 2018年5月30日,今天遇到一个关于串口协议相关的问题,其中涉及到串口传输工具sz,rz等的使用,从man手册中并没有获取到有效信息,因此经过一番搜索,才知这两个工具应该这样使用 二.使用 ...

  9. javaweb项目运行时生成的Servers项目作用

    在javaweb项目中,看到有一个Servers的项目,发现每新增一个项目,就会在Servers项目中新生成一些对应的项目文件. 如图所示: 每个项目都有对应的文件.文件的结构图如下: 解释一:Ser ...

  10. Python CSV Reader/Writer 例子--转载

    CSV(comma-separated values) 是跨多种形式导入导出数据的标准格式,比如 MySQL.Excel. 它以纯文本存储数和文本.文件的每一行就代表一条数据,每条记录包含了由逗号分隔 ...