【Substring with Concatenation of All Words】cpp
题目:
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;
}
};
【Substring with Concatenation of All Words】cpp的更多相关文章
- 【Flatten Binary Tree to Linked List】cpp
题目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...
- 【Binary Tree Zigzag Level Order Traversal】cpp
题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 【Kth Smallest Element in a BST 】cpp
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
- 【Search in Rotated Sorted Array II 】cpp
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 【Letter Combinations of a Phone Number】cpp
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 【Remove Duplicates from Sorted List II 】cpp
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【Remove Duplicates from Sorted Array II】cpp
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【leetcode】Substring with Concatenation of All Words
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
随机推荐
- 【转】android布局--Android fill_parent、wrap_content和match_parent的区别
三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便. 1)fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以 ...
- 1006: Hero In Maze
1006: Hero In Maze 时间限制: 1000 Sec 内存限制: 64 MB提交: 417 解决: 80[提交][状态][讨论版][命题人:外部导入] 题目描述 500年前,Jess ...
- 关于VMware给系统分区扩容的一点经验
我的VMware版本是8.0.6 build-1035888,里面安装的是Windows XP SP3 首先,在VM关机状态下使用Hard disk设置中的Utilities下的Expand给整个磁盘 ...
- 剑指offer25 二叉树中和为某一直的路径
先序遍历 class Solution { public: vector<vector<int> > FindPath(TreeNode* root,int expectNum ...
- ajax实现无刷新两级联动DropDownList
ajax实现的无刷新三级联动 http://zhangyu028.cnblogs.com/articles/310568.html 本文来自小山blog:http://singlepine.cnblo ...
- ReactiveCocoa实战: 模仿 "花瓣",重写 LeanCloud Rest Api的iOS REST Client.
这一次我们将要讨论的是移动开发中比较重要的一环--网络请求的封装.鉴于个人经验有限,本文将在一定程度上参考 基于AFNetworking2.0和ReactiveCocoa2.1的iOS REST Cl ...
- 39条常见的Linux系统简单面试题
39条常见的Linux系统简单面试题 本文主要分享39条常见的Linux系统简单面试题,其中包括如何看当前Linux系统有几颗物理CPU和每颗CPU的核数.如何实时查看网卡流量为多少等等,希望对你有所 ...
- 交换机基础配置之三层交换机实现vlan间通信
我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...
- Java OOP——第七章 多线程
1.进程:是指运行中的应用程序,每个进程都有自己独立的地址空间(内存空间): Eg:用户点击桌面的IE浏览器,就启动了一个进程,操作系统就会为该进程分配独立的地址空间.当用户再次点击左面的IE浏览器, ...
- (转)Unity 和 Cocos2d-x 越渐流行,国内公司开发「自研游戏引擎」的意义何在?
分几个角度来说:一.我认为,Unity3D将无可挽回的,或者说,势在必得的,成为接下来很多年内,世界移动领域游戏引擎市场霸主.回顾历史,正如同咱们经历过一次又一次的互联网时代变革一样,x86,wind ...