题目:

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

代码:

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> ret;
if ( words.empty() || s.empty() ) return ret;
const int len_w = words[].size(); // length of substring in words
const int end = words.size()*len_w; // total length of words
if ( end>s.size() ) return ret; // s size not enough
map<string, int> ori;
for ( int i=; i<words.size(); ++i )
{
if ( ori.find(words[i])==ori.end() ){
ori[words[i]] = ;
}
else{
ori[words[i]]++;
}
}
map<string, int> found;
int match_num = ;
int begin = ;
int i = ;
while ( i<s.size() )
{
//cout << "i:" << i << endl;
//cout << "m:" << match_num << endl;
//cout << "begin:" << begin << endl;
// match one substring in words
if ( ori.find(s.substr(i,len_w))!=ori.end() )
{
found[s.substr(i,len_w)]++;
// substring occur more than times in words
if ( found[s.substr(i,len_w)]>ori[s.substr(i,len_w)] )
{
found.clear();
match_num = ;
begin++;
i=begin;
continue;
}
i = i+len_w;
match_num++;
//cout << match_num << endl;
// match all substrings in words and push back a starting indices
if ( match_num==words.size() )
{
ret.push_back(i-end);
found.clear();
begin++;
i = begin;
match_num=;
}
}
// not match
else
{
found.clear();
match_num = ;
begin++;
i=begin;
}
}
return ret;
}
};

tips:

采用双指针技巧。

维护几个关键变量:

1. begin:可能的有效开始位置

2. match_num: 已经匹配上的words中的个数

3. ori: words中每个string,及其出现的次数

4. found: 当前已匹配的interval中,words中每个string,及其出现的次数

思路就是如果找到了匹配的某个string,i就不断向后跳;跳的过程中可能有两种情况不能跳了:

a) 下一个固定长度的子字符串不匹配了

b) 下一个固定长度的子字符串虽然匹配上了words中的一个,但是匹配的数量已经超过了ori中该string的数量

如果一旦不能跳了,就要把match_num和found归零;而且需要回溯到begin++的位置,开始新一轮的搜寻。

这里有个细节要注意:

直接用found.clear()就可以了,如果一个一个清零,会超时。

======================================

第二次过这道题,代码简洁了,一些细节回忆着也写出来了。保留一个ori用于判断的,再维护一个wordHit用于计数的;ori不能动,wordHit每次clear后默认的value=0。

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> ret;
if ( words.empty() ) return ret;
int len = words[].size();
if ( len*words.size()>s.size() ) return ret;
map<string, int> wordHit;
map<string ,int> ori;
for ( int i=; i<words.size(); ++i ) ori[words[i]]++;
for ( int i=; i<=s.size()-len*words.size(); ++i )
{
wordHit.clear();
int j = i;
int hitCouts = ;
while ( hitCouts<words.size() && j<=s.size()-len )
{
// find word and not hit already
if ( ori.find(s.substr(j,len))==ori.end() ) break;
if ( wordHit[s.substr(j,len)]<ori[s.substr(j,len)] )
{
wordHit[s.substr(j,len)]++;
j += len;
hitCouts++;
}
else { break; }
}
// if hits all words
if ( hitCouts==words.size() ) ret.push_back(i);
}
return ret;
}
};

最新文章

  1. ThinkPHP3快速入门教程二:数据CURD
  2. 小游戏Talk表
  3. Merge在Sqlserver使用例子说明
  4. Scrum - BB项目日志
  5. MediaWiki隐藏index
  6. ThinkPHP 3.2.3 简单后台模块开发(一)常用配置
  7. redis 安装及相关问题解决
  8. 利用SSIS发送邮件
  9. 标题右边10px位置紧跟发布时间
  10. 网络配置之基本网络配置(cenos6)
  11. synchronized和volatile比较
  12. Learning ROS for Robotics Programming Second Edition学习笔记(三) indigo rplidar rviz slam
  13. svn如何根据提交日志信息回退版本
  14. bak
  15. BZOJ1477 青蛙的约会 扩展欧几里德
  16. BZOJ1856或洛谷1641 [SCOI2010]生成字符串
  17. Razor 中的@rendersection
  18. 可嵌入图片视频jQuery全屏滑块
  19. 第一次spring冲刺第3、4天
  20. JS知识点整理(一)

热门文章

  1. SQL-常用数据类型
  2. 2019.03.13 ZJOI2019模拟赛 解题报告
  3. 2017.11.6 JavaWeb-----第七章 JavaWeb常用开发模式与案例
  4. JS面向对象、prototype、call()、apply()
  5. Mybatis-generator自动生成
  6. CUDA Texture纹理存储器 示例程序
  7. 2.Netty的粘包、拆包(一)
  8. ES5 与 ES6六大不同
  9. ES6初识-(冲突)数据结构
  10. spring-JDBC Template