leetcode第29题--Substring with Concatenation of All Words
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的更多相关文章
- 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 ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- 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 ...
- 乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words
乘风破浪:LeetCode真题_030_Substring with Concatenation of All Words 一.前言 这次我们还是找字符串的索引,不过,需要将另一个字符串列表中的 ...
- 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 ...
- 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 ...
- [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 ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- 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 ...
随机推荐
- Cloudera impala简单介绍及安装具体解释
一.Impala简单介绍 Cloudera Impala对你存储在Apache Hadoop在HDFS,HBase的数据提供直接查询互动的SQL.除了像Hive使用同样的统一存储平台,Impala也使 ...
- R0-R37它是Arm 寄存器,那是,CPU内部。和GPIO注册所有外设。换句话说,要是arm的cpu,它包含了其他芯片公司将有R0-R37,和GPIO寄存器只有一个特定的芯片。
R0-R37它是Arm 寄存器.那是,CPU内部.和GPIO注册所有外设. 换句话说,要是arm的cpu,它包含了其他芯片公司将有R0-R37,和GPIO有. 版权声明:本文博主原创文章.博客,未经同 ...
- Android MenuItem 设置文本颜色-TextColor设置
前面一直在寻找 MenuItem文字颜色设置. 我发现API唯一的背景颜色设置. .. 因此,找到下面的方法.在OverFlow看到. 在onCreateOptionsMenu一下. 使MenuIte ...
- zoj 3829 Known Notation(2014在牡丹江区域赛k称号)
Known Notation Time Limit: 2 Seconds Memory Limit: 131072 KB Do you know reverse Polish notatio ...
- SyntaxHighlighter代码高亮插件
SyntaxHighlighter它是Google Code在一个开源项目,主要用于对代码着色页, 使用十分方便,效果也不错,并且差点儿支持常见的全部语言. 使用步骤: 一.下载并解压缩SyntaxH ...
- 利用修改div的位置+js对象存储div信息 实现简单的div自定义布局功能
原文:利用修改div的位置+js对象存储div信息 实现简单的div自定义布局功能 利用修改div的位置+js对象存储div信息 实现简单的div自定义布局功能1.在界面上添加几个checkbox和一 ...
- EasyUI 扩展自己定义EasyUI校验规则 验证规则(经常使用的)
比如 校验输入框仅仅能录入0-1000之间 最多有2位小数的数字 表单<input type="text" id="rate" name="ra ...
- 一些有用的javascript实例分析(二)
原文:一些有用的javascript实例分析(二) 5 求出数组中所有数字的和 window.onload = function () { var oBtn = document.getElement ...
- rdlc报告vs2008编辑正常,在vs2012在对错误的编辑
最近我们的系统开发的工具vs2008升级到2012,由于系统是非常的报告是由rdlc发展.今天 有需要修改的报告满足需求.直接使用vs2012正确rdlc报告编辑,结果本次变动后.报表都报错. 后来我 ...
- hdu Just a Hook
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树+lazy操作 线段树是从上到下开始建树,树状数组是从下到上建树.... 代码: ...