problem:

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

题目的意思是,给定S和字符串数组L,判断S中是否存在包含L中的所有字符串,字符串的顺序不做要求,但是字符串之间不得有其他字符,如果存在,返回S中的下标。例如“abcba”,L为“a”,“b”,那么返回0和3. 还有就是允许之间的重复,也就是“aba”,L为“a”,“b”时,返回为0和1,其中b被重复考虑。

做法是这样的,先创建两个map<string, int> 一个是用来计算L中每种串的出现次数。另外一个是用来在遍历过程中判断超出了第一个map表记录的L中的次数。第二个map记得每次重新遍历的时候要清空。详细代码如下:

class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L)
{
vector<int> ans;
if (S.size() == || L.size() == || S.size() < L.size() * L[].size())
return ans;
int wordNum = L.size();
int wordLen = L[].size();
int searchEnd = S.size() - wordNum * wordLen;
map<string, int> total;
map<string, int> subMap; for(int i = ; i < wordNum; i++)
{
total[L[i]]++;
} for (int i = ; i <= searchEnd; i++) // 等号不能少了,还有最后就是用searchEnd代替S.size() - wordNum*wordLen;
{
int matchNum = , j = i;
subMap.clear();// 记得清空
for (; matchNum < wordNum; matchNum++)
{
string subs = S.substr(j, wordLen);
if (total[subs] == )
break;
if (++subMap[subs] > total[subs])
break;
j += wordLen;
}
if (matchNum == wordNum)
ans.push_back(i);
}
return ans;
}
};

还可以参见http://www.tuicool.com/articles/Uza2eui;java的参见http://blog.csdn.net/linhuanmars/article/details/20342851这个方法比较快,有时间一定要学学。

leetcode第29题--Substring with Concatenation of All Words的更多相关文章

  1. LeetCode 笔记系列七 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 star ...

  2. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  3. LeetCode(30) Substring with Concatenation of All Words

    题目 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  4. 乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words

    乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words 一.前言    这次我们还是找字符串的索引,不过,需要将另一个字符串列表中的 ...

  5. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

  6. leetcode面试准备: Substring with Concatenation of All Words

    leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...

  7. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  8. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  9. LeetCode HashTable 30 Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

随机推荐

  1. Windows下一个SlikSVN使用

    我相信所有的应SVN不熟悉.使用过.可是并非人人都自己配置过SVNserver.以下就是我配置SVNserver的步骤.以及在配置过程中碰见的一些问题,在此记录,希望对你有所帮助. 安装 双击执行&q ...

  2. Java NIO系列教程(三) Buffer(转)

    Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...

  3. opengl 扳回一球

    本文介绍了使用两种方法opengl画一个球体,一个是一个球形点位置计算,然后绘制出,还有一个glut套件自带功能. 一.直接绘制法 直接贴代码,解释都写在凝视里了.绘制时先移动好坐标系.然后调用这方法 ...

  4. git fetch, merge, pull, push需要注意的地方(转)

    在git操作中,我们经常会用到fetch, merge, pull和push等命令,以下是一些我们需要注意的地方. 给大家准备了参考资料: 1. Whatʼs a Fast Forward Merge ...

  5. HDU ACM 1068 最大独立集

    意甲冠军:n同学.有些学生将有宿命的男性和女性成为恋人.收集注定要成为爱好者求学生的最大数目不存在. 分析:独立设置,顶点设定图的一个子集,在休闲2不连续: 二分图:最大独立集 = 顶点 - 匹配的最 ...

  6. Java设计模式(七)策略模式 模板模式

    (十三)策略模式 策略图案限定了多个封装算法,该算法可以相互替换包.法的客户.借用还有一位大神的样例. interface ICalculator{ public int calculate(Stri ...

  7. Dos命令将合并两个文本文件的内容

    当生产线的问题,有一个放b.txt的内容被添加到a.txt这需要采取.在考虑这个问题.我的第一感觉是敲代码.阅读b.txt内容,渐进写a.txt.想起昨天在加工处理生产线600M决的方法,我用java ...

  8. 第22章 职责链模式(Chain of Responsibility)

    原文 第22章 职责链模式(Chain of Responsibility) 职责链模式 导读:职责链模式是一个既简单又复杂的设计模式,刚开始学习这个设计模式的时候光示例都看了好几遍.就为了理清里面的 ...

  9. Web字体@font-face对于中文字体的使用

    今天算是刚开始玩博客园..感觉很新鲜在首页 上看到了一个博客http://www.cnblogs.com/liuminghai/p/4238256.html是关于web文字的,挺不错 但是B/S的前端 ...

  10. Visio Premium 2010钥匙+激活破解方法

    Visio Premium 2010钥匙+激活破解方法: 安装时能够使用的关键: GR24B-GC2XY-KRXRG-2TRJJ-4X7DC VWQ6G-37WBG-J7DJP-CY66Y-V278X ...