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. 解决win10客户机本地账户登陆导致远程桌面没法访问问题

    情景:客户机器如果是win10本地账户,我们远程桌面连接是可能会没法访问. 如果客户机器切换到win10 Microsoft账户登录,远程桌面就可以访问了(当然用户肯定不能给你说自己的Microsof ...

  2. SpringAop进行日志管理。

    在java开发中日志的管理有非常多种.我通常会使用过滤器,或者是Spring的拦截器进行日志的处理.假设是用过滤器比較简单,仅仅要对全部的.do提交进行拦截,然后获取action的提交路径就能够获取对 ...

  3. Instll meld in windows

    在linux下用meld感觉颇为不错,但是在windows上真是折腾老久.p4merge也试了试, 但还是meld用起来更顺手. 使用 https://wiki.gnome.org/Meld/Wind ...

  4. UC浏览器插件开发

    pip install UC浏览器插件是个什么玩意? 如图所看到的,便是UC的插件面板. UC通过开放浏览器插件api, 使开发人员可以进行插件的开发. 插件种类: 1 : extension. 就是 ...

  5. NSIS:使用WinVer.nsh头文件判断操作系统版本

    原文 NSIS:使用WinVer.nsh头文件判断操作系统版本 AtLeastWin<version> 检测是否高于指定版本 IsWin<version> 检测指定版本(唯一限 ...

  6. svn常见错误汇总

    comment中的换行.把换行去掉就可以了

  7. docker搭建基础的tomcat应用

    tomcat server是眼下比較流行的开源中间件server,以下介绍怎样使用 docker 来做一个 tomcat 数据库服务.官方的仓里没有标 OFFICIAL 的 tomcat 的镜像,只是 ...

  8. 解析grant connect, resource to user语句

    今天同事问了一个问题:"创建用户分配的权限是:grant connect,resource to user;,可是建立view的时候失败了,错误是权限不够,后来我给这个用户分配了创建view ...

  9. 汉高澳大利亚sinox2014电影播放flash最好的办法是安装游戏windows文本firefox

    事实上,韩澳sinox本身是没有原生flashplayer,无论怎么捣鼓,它们是从adobe弄linux要么windows版本号flashplayer,它不停地拨弄linux版本号flashplaye ...

  10. JNDI数据源配置注意事项

    假设是在原有project上改动 1,applicationContext.xml中改动当中的value值 <bean id="dataSource" class=" ...