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

 
第一种简单的思路:
用两个map,一个map用于表示L中各个单词的数目,另一个map用于统计当前遍历S得到的单词的数目
 
从第0个,第1个,第2个位置开始遍历S串
统计得到的单词的数目,如果某个单词数目超出了L中该单词的数目,或者某个单词没有再L中出现,则重新从下一个位置开始统计
 
 class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) { if(L.size()==) return vector<int>(); int wordLen=L[].size(); map<string,int> wordCount; int i,j;
for(i=;i<L.size();i++) wordCount[L[i]]++; vector<int> result;
map<string,int> counting; for(i=;i<(int)S.length()-wordLen*L.size()+;i++)
{
counting.clear();
for(j=;j<L.size();j++)
{
string str=S.substr(i+j*wordLen,wordLen);
if(wordCount.find(str)!=wordCount.end())
{
counting[str]++;
if(counting[str]>wordCount[str])
{
break;
}
}
else
{
break;
}
} if(j==L.size())
{
result.push_back(i);
//i=i+L.size()*wordLen-1;
}
} return result; }
};
 
 
 
第二种思路,维护一个窗口,在遍历时分别要从0,1……wordLen开始遍历一遍,防止遗漏
 
如果发现某个单词不在L中,则从下一个位置开始,重新统计
如果发现某个单词出现的次数多了,则需从这个单词第一次出现位置的后面一位开始统计,并要剔除这个位置之前统计的一些结果
 
 class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) { if(L.size()==) return vector<int>(); int wordLen=L[].size(); map<string,int> wordCount; int i,j;
for(i=;i<L.size();i++) wordCount[L[i]]++; vector<int> result;
map<string,int> counting; int count=;
int start; for(i=;i<wordLen;i++)
{
count=;
start=i;
counting.clear();
for(int j=i;j<S.length()-wordLen+;j=j+wordLen)
{
string str=S.substr(j,wordLen); if(wordCount.find(str)!=wordCount.end())
{
counting[str]++;
count++;
if(counting[str]>wordCount[str])
{
//更新start位于str第一次出现之后,更新counting,更新count
getNextIndex(start,str,counting,S,wordLen,count);
}
}
else
{
counting.clear();
start=j+wordLen;
count=;
} if(count==L.size())
{
result.push_back(start);
}
}
}
return result;
} void getNextIndex(int &start,string str,map<string,int> &counting,string &S,int &wordLen,int &count)
{
for(int j=;;j++)
{
string tmpStr=S.substr(start+j*wordLen,wordLen);
count--;
counting[tmpStr]--;
if(tmpStr==str)
{
start=start+(j+)*wordLen;
break;
}
} }
};
 
 
 

【leetcode】Substring with Concatenation of All Words的更多相关文章

  1. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  2. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  3. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  4. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  5. 【leetcode】Find All Anagrams in a String

    [leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...

  6. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  7. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  8. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. ASP.NET MVC使用Bootstrap系列(4)——使用JavaScript插件

    阅读目录 序言 Data属性 VS 编程API 下拉菜单(dropdown.js) 模态框(modal.js) 标签页(tab.js) 工具提示(tooltip.js) 弹出框(popover.js) ...

  2. svn强制解锁的几种做法

    标签: svn强制解锁 2013-12-16 17:40 12953人阅读 评论(0) 收藏 举报  分类: SoftwareProject(23)  版权声明:本文为博主原创文章,未经博主允许不得转 ...

  3. Redis3.0.7 cluster/集群 安装配置教程

    1.前言 环境:CentOS-6.7-i386-LiveDVD 安装的CentOs系统 节点: 6个节点,3个主节点.3个从节点(由于redis默认需要3个主节点,如果想每个主节点有一个从节点,这是最 ...

  4. Java 线程Thread.Sleep详解

    我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢? 思考下面这两个问题: 1.假设现在是 2008-4-7 12:00:00.000,如果 ...

  5. 来自 Google 的 R 语言编码风格指南

    来自 Google 的 R 语言编码风格指南R 语言是一门主要用于统计计算和绘图的高级编程语言. 这份 R 语言编码风格指南旨在让我们的 R 代码更容易阅读.分享和检查. 以下规则系与 Google ...

  6. 关于Windows下安装mongodb和加入Windows系统启动项

    .首先:在http://www.mongodb.org/downloads官网下载最新的win版本的mongodb下载包(我下载到d盘) .加压缩,修改文件夹名字为mongodb,建立放数据库文件夹w ...

  7. Bots(逆元,递推)

    H. Bots time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input out ...

  8. 【PHP面向对象(OOP)编程入门教程】9.封装性(var与public,protected,private的关系)

    封装性是面象对象编程中的三大特性之一,封装性就是把对象的属性和服务结合成一个独立的相同单位,并尽可能隐蔽对象的内部细节,包含两个含义: 1. 把对象的全部属性和全部服务结合在一起,形成一个不可分割的独 ...

  9. 区别 PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input

    Mac Pro 电脑编译安装了 PHP 5.6.21,先前的系统运行时报如下警告级错误: Deprecated: Automatically populating $HTTP_RAW_POST_DAT ...

  10. 计算字符数组长度,用strlen 与 sizeof 的原理与区别

    遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...