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. android自定义控件(8)-利用onMeasure测量使图片拉伸永不变形,解决屏幕适配问题

    使用ImageView会遇到的问题 在Android应用中,都少不了图片的显示,ImageView,轮播图,ViewPager等等,很多都是来显示图片的,很多时候,我们都希望图片能够在宽度上填充父窗体 ...

  2. the usage of linux command "expect"

    #! /usr/bin/expect -f# this script is used to practise the command "expect" #when "li ...

  3. 简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下   本文实例讲述了简单实用的PHP防注 ...

  4. PHP简单漂亮的分页类

    本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap. <?php /* * ********************************************* * @类名 ...

  5. sql事务和锁

    摘自:http://www.cnblogs.com/lxconan/archive/2011/10/20/sql_transaction_n_locks_1.html 最近在项目中进行压力测试遇到了数 ...

  6. Reading Famous blog to prevent me wasting time on blind wandering

    I can`t help surfing the useless bbs and some other kind of SNS. The time I begin to do it, it costs ...

  7. oracle数据表创建分区与查询

    场景: 遇到1亿数据量的数据需要根据用户名做些数据统计分析,想直接做些聚合计算基本没可能,于是打算先根据日期按照年月创建分区,然后对各个分区分别进行统计,最后汇总结果. 有两种方法,分别是手工设置分区 ...

  8. Redis学习笔记八:独立功能之二进制位数组

    Redis 提供了 setbit.getbit.bitcount.bitop 四个命令用于处理二进制位数组. setbit 命令用于为位数组指定偏移量上的二进制位设置值,偏移量从 0 开始计数. ge ...

  9. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  10. 通过Unity3d创建二维码(利用zxing2.2)

    http://blog.csdn.net/liulala16/article/details/14521979 2013-11-08 14:53 1965人阅读 评论(3) 收藏 举报 首先 下载ZX ...