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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
For example, given:
s: “barfoothefoobarman”
words: [“foo”, “bar”]
You should return the indices: [0,9].
(order does not matter).
分析
解决该问题的关键是理解清楚要求。
给定一个目标字符串s,一个单词集合words。
要求使得words集合中所有元素连续出现在s中的首位置组成的集合(元素顺序不考虑)。
正如所给实例,目标字符串s: “barfoothefoobarman”
对比单词集合words: [“foo”, “bar”]
我们发现,在pos=0 ~ 5时“barfoo”恰好匹配,则0压入结果vector;
在pos=9 ~ 14时“foobar”恰好匹配,则9压入结果vector;
在理清楚题意后,便可入手程序实现。
AC代码
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
if (words.empty())
return vector<int>();
vector<int> ret;
//记录所给words中每个单词的出现次数
map<string, int> word_count;
//每个单词的长度相同
int word_size = strlen(words[0].c_str());
int word_nums = words.size();
//所给匹配字符串的长度
int s_len = strlen(s.c_str());
for (int i = 0; i < word_nums; i++)
++word_count[words[i]];
int i, j;
map<string, int> temp_count;
for (i = 0; i < s_len - word_nums*word_size + 1; ++i)
{
temp_count.clear();
for (j = 0; j < word_nums; j++)
{
//检验当前单词是否属于words以及出现的次数是否一致
string word = s.substr(i + j*word_size, word_size);
if (word_count.find(word) != word_count.end())
{
++temp_count[word];
//如果出现的次数与words不一致,则返回错误
if (temp_count[word] > word_count[word])
break;
}//if
else{
break;
}//else
}//for
//所有words内的单词,在i起始位置都出现,则将下标i存入结果的vector中
if (j == word_nums)
{
ret.push_back(i);
}//if
}//for
return ret;
}
};
LeetCode(30) Substring with Concatenation of All Words的更多相关文章
- LeetCode(30):与所有单词相关联的字串
Hard! 题目描述: 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...
- Spring3整合Hibernate4-我们到底能走多远系列(30)
我们到底能走多远系列(30) 扯淡: 30篇啦!从2012-08-15开始的系列,东平西凑将近一年的时间也就这么几篇.目标的100篇,按这个速度也要再搞两年呢. 发博客果然不是件容易的事,怪不得更多的 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言) 我们的系统有时要扩展到其他国家,或者地区,需要更多的语言环境,微软提供了一些解决 ...
- Windows Phone开发(30):图形
原文:Windows Phone开发(30):图形 图形如矩形.椭圆.路径等都从Shape类派生,它们一般表示规则或不规则图形,这些图形都是简单的二维图形,我相信大家都能理解的. 例一:矩形. 请看下 ...
- Qt 学习之路 2(30):Graphics View Framework
Qt 学习之路 2(30):Graphics View Framework 豆子 2012年12月11日 Qt 学习之路 2 27条评论 Graphics View 提供了一种接口,用于管理大量自定义 ...
- pat06-图6. 公路村村通(30)
06-图6. 公路村村通(30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
随机推荐
- Ocelot(七)- 入门
入门 Ocelot仅适用于.NET Core,目前是为netstandard2.0构建的.如果Ocelot适合您,那么此文档可能会有用. .NET 安装NuGet包 使用nuget安装Ocelot及其 ...
- 题解报告:hdu 1039 Easier Done Than Said?
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1039 Problem Description Password security is a trick ...
- Sql Server把本地数据库传到服务器数据库
上一篇文章我们已经把网站布署到服务器中了,如果是动态网站肯定是有数据库的,接下来通过Sql Server把本地数据库上传到服务器数据库中. 打开Sql Server连接本地数据库,选中要导出的数据库, ...
- 在Stuts2中使用ModelDriven action
在Struts2中,提供了另外一种直接使用领域对象的方式,那就是让action实现com.opensymphony.xwork2.ModelDriven接口.ModelDriven让你可以直接操作应用 ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第五天(非原创)
文章大纲 一.课程介绍二.前台系统(门户系统)搭建介绍三.前台系统(门户系统)搭建实战四.js请求跨域解决五.项目源码与资料下载六.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背 ...
- UnixTime的时间戳的转换
public static string XConvertDateTime(double unixTime) { System.DateTime time = System.DateTime.MinV ...
- iOS/Android 视频编辑SDK
锐动天地为开发者提供短视频编辑.特效.直播.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨并创新技术,在稳定性,兼容性,硬件设备效率优化上千捶百 ...
- greenplum4.3.8.2安装
GREENPLUM总体结构: 数据库由Master Severs和Segment Severs通过Interconnect互联组成. Master主机负责:建立与客户端的连接和管理:SQL的解析并 ...
- 3D旋转矩阵的推导过程
3D旋转矩阵的推导过程 包含平移的线性变换称作仿射变换,3D中的仿射变换不能用 3 x 3 矩阵表达,必须使用4 x 4矩阵. 一般来说,变换物体相当于以相反的量变换描述这个物体的坐标系.当有多个变换 ...
- Nexus环境搭建
安装 1.解压nexus-2.11.01-bundle.zip到F:\Java\nexus(可自定义) 2.进入F:\Java\nexus\nexus-2.11.1-01\bin\jsw进入相应的系统 ...