【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 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).
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;
}
};
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的更多相关文章
- 【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 ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【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 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- Saltstack常用模块及API
Saltstack提供了非常丰富的功能模块,涉及操作系统的基础功能.常用工具支持等,更多模块信息可以查看官网模块介绍.也可以通过sys模块列出当前版本支持的模块. salt '*' sys.list_ ...
- 浅谈T-SQL中的联接查询
引言 平时开发时,经常会使用数据库进行增删改查,免不了会涉及多表联接.今天就简单的记录下T-SQL下的联接操作. 联接类型及其介绍 在T-SQL中联接操作使用的是JOIN表运算符.联接有三种基本的类型 ...
- SCP 命令(转)
\ svn 删除所有的 .svn文件 find . -name .svn -type d -exec rm -fr {} \; linux之cp/scp命令+scp命令详解 名称:cp 使用权限: ...
- Android布局属性大全
RelativeLayout http://wenku.baidu.com/view/2e39724333687e21af45a97e.html?from=related&hasrec=1 h ...
- 常见的几个angular.js的问题
来源于网络收集 一.ng-show/ng-hide 与 ng-if的区别? 我们都知道ng-show/ng-hide实际上是通过display来进行隐藏和显示的.而ng-if实际上控制dom节点的增删 ...
- 【CISP笔记】安全攻击与防护
公开信息收集-搜索引擎 快速定位Google 搜索“5sf67.jsp”可以找到存在此脚本的Web网站Google 搜索“teweb/default.htm”就可找到开放着远程Web连接的服务器信息挖 ...
- seo是什么职业
SEO(Search Engine Optimization)汉译为搜索引擎优化.seo从业者首要工作就是优化网站,使其符合搜索引擎的基本规律和满足用户的需求,进而获得大量的用户访问.SEO职业属于一 ...
- NHibernate配置
因为NHibernate被设计为可以在许多不同环境下工作,所以它有很多配置参数.幸运的是,大部分都已经有默认值了. NHibernate.Test.dll包含了一个示例的配置文件app.config, ...
- CSS只是要点-收集
1. CSS 浮动定位详解 请点击:css浮动定位详解
- eclipse如何导入java项目文件
平时下载到项目时,希望能够导入到eclipse中使用.但有些项目不能直接导入,需要做转换. 打开源文件目录,查询如下: 如果目录中包含pom.xml文件,则说明该项目由Maven构建的,参考以下 如何 ...