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. LSM存储模型

    LSM存储模型 数据库有3种基本的存储引擎: 哈希表,支持增.删.改以及随机读取操作,但不支持顺序扫描,对应的存储系统为key-value存储系统.对于key-value的插入以及查询,哈希表的复杂度 ...

  2. Python之路【第十六篇】Django基础

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  3. 固定导航(Sticky nav)

    方法1: <div class="footer"></div> .footer{ position:fixed; bottom:0; left:0; wid ...

  4. 基于php使用memcache存储session的详解(转)

    web服务器的php session都给memcached ,这样你不管分发器把 ip连接分给哪个web服务器都不会有问题了,配置方法很简单,就在php的配置文件内增加一条语句就可以了,不过前提你需要 ...

  5. 小技能——markdown

    如果常常要在电脑上写点东西,比如写笔记.做总结.写博客之类的,花一两个小时学会markdown还是很值的. markdown简介 markdown不是某个软件,而是一种标记语言,标记普通文本的格式,以 ...

  6. Git工作流总结

    引用自:https://github.com/xirong/my-git/blob/master/git-workflow-tutorial.md 说明: 个人在学习Git工作流的过程中,从原有的 S ...

  7. ROS之VPN服务器设置教程.

    关于ROS系统的安装此处将不再累述,可以自行谷歌,百度搜索“ROS 安装配置教程”. (安装方法可以使用光盘安装,USB引导安装,硬盘写入.) 好了,演示创建VPN服务器的方法: 1.使用WinBox ...

  8. 5、数组和集合--Collection、Map

    一.数组:同一个类型数据的集合,其实他也是一个容器 1.数组的好处:可以自动给数组中的元素从0开始编号,方便操作这些数据 2.数组的定义: 在Java中常见: 格式1:  类型 [] 数组名 = ne ...

  9. C#3.0 特性

    C#3.0特性 隐式类型的本地变量和数组 对象初始值设定项 集合初始值设定项 扩展方法 匿名类型 lambda表达式 查询关键字 自动实现的属性 分布方法定义 lambda表达式与表达式树 https ...

  10. 第2月第3天 egorefresh

    egorefresh是很老的下拉刷新,它是一个uiview,在uitableview 下拉的时候显示不同的界面. egorefresh和uitableview的耦合度很高,uitableview滚动和 ...