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. ecshop广告调用方法

    在简单地概括ecshop广告调用该方法,已发表在博客上,在这里,我们总结了以下 :就是官方默认的方法.先加入广告位,然后加入模板的广告位区域,再在将两者相应上. 1.后台 > 广告管理 > ...

  2. Gradle digest

    task类型 copy task copyFiles(type: Copy) { from 'resources' into 'target' include '**/*.xml', '**/*.tx ...

  3. ffmpeg.c简单的结构功能分析(平局)

    当转码的研究看前一阵子FFmpeg资源. 因为ffmpeg.c与此相反的较长的代码.而有相当一部分人AVFilter相关代码(这部分已经不太熟悉),所以学习之前FFmpeg时间,还没有好好看看它的源代 ...

  4. 使用Advanced Installer 自动部署 Arcgis Engine Runtime 10.0

    原文:使用Advanced Installer 自动部署 Arcgis Engine Runtime 10.0 目前采用Arcgis9.2 + c#(vs2008)作为程序开发平台,是一个不错的搭配. ...

  5. response的contentType 几种类型

    ajax开发中在请求server端的响应时, 对于每一种返回类型 规范的做法是要在服务端指定response的contentType 的. (当然 不指定绝大多数情况下也没什么问题 尤其是返回&quo ...

  6. 网络编程easy错误点-手知道

    通常的网络编程socket编程.实际上.socket编程并不仅仅是满足网络间不同主机之间的通信,它也能实现同一台主机上不同进程间的通信需求. 其体如今创建socket时的參数的不同: int sock ...

  7. jquery动态刷新局部表单

    想实现一个效果就是选择某个年份:然后再action中按该年份查找数据库中的数据,返回到页面表单中显示. 1.添加登记年度的changge事件,也是异步请求. $(document).ready(fun ...

  8. JS中call、apply的用法说明

    JS Call()与Apply()的区别 ECMAScript规范给所有函数都定义了Call()与apply()两个方法,call与apply的第一个参数都是需要调用的函数对象,在函数体内这个参数就是 ...

  9. 使用javascript实现的一些功能

    原文:使用javascript实现的一些功能 今天学习了javascript中的事件,已经接近尾声,可以说明天跨入jquery的学习啦,学习了一周的javascript,感觉还没有掌握其中学习的微妙之 ...

  10. 【转】传递给Appium服务器以开启相应安卓Automation会话的Capabilities的几点说明

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39431307 Desired Capabilities是由客户端发送给Appium服务器端 ...