【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 ...
随机推荐
- Lua弱引用table
弱引用table 与python等脚本语言类似地,Lua也采用了自动内存管理(Garbage Collection),一个程序只需创建对象,而无需删除对象.通过使用垃圾收集机制,Lua会自动删除过期对 ...
- C语言二维数组中的指针问题
#include "stdio.h" void main() { int a[5][5]; int i,j; for (i=0;i<5;i++) { for (j=0;j&l ...
- jQuery 取值、赋值的基本方法
转载:http://www.cnblogs.com/huanhuan86/archive/2012/06/13/2548071.html 获取元素的value值: /*获得TEXT.AREATEXT的 ...
- Win7电脑无法启用无线连接或无线连不上网
1. 上不去网,看是否是无线网卡禁止. 2. 打开控制面板--网络和Internet--查看网络状态和任务--更改网络适配器 3. 4.若启用后无线网络连接仍为灰色,继续往下看 5.点击开始,找到运行 ...
- [转]Ubuntu 16.04建议安装
Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...
- thinkphp-许愿墙-3
用jquery写异步传递的时候, 首先要判断表单中的输入是否为空: 如果有多个输入项, 应该, 分别的, 一步一步的来判断是否为空, 而不是用 and / or来复合判断! 同时如果为空, 应该将它设 ...
- edwin报警和监控平台开源了(python源码)
简单介绍一下edwin edwin是一个报警和监控平台, 可以使用它监控任意东西, 如有异常(分为警告级和严重级), 可以发出报警. 可以自定义报警的通知方式, 比如邮件/短信/电话. 另外, 它提供 ...
- JQuery中的html(),text(),val()区别
jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...
- 解析posix与perl标准的正则表达式区别 ---PHP
正则表达式(Regular Expression,缩写为regexp,regex或regxp),又称正规表达式.正规表示式或常规表达式或正规化表示法或正规表示法,是指一个用 来描述或者匹配一系 ...
- lustre文件系统部署流程
# 1 准备工作### 1.1 添加以太网址添加以太网地址,使得gio017可以访问到需要安装的节点.修改gio017上的/etc/hosts,将需要批量操作的节点名以如下方式添加.```[gio01 ...