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 ...
随机推荐
- 【牛客-14602】xinjun与阴阳师(01背包)
xinjun与阴阳师 题目描述 xinjun是各类手游的狂热粉丝,因随手一氪.一氪上千而威震工大,现在他迷上了阴阳师.xinjun玩手游有一个习惯,就是经过层层计算制定出一套方案来使操作利益最大化(因 ...
- That Nice Euler Circuit UVALive - 3263 || 欧拉公式
欧拉定理: 简单多面体的顶点数V.棱数E及面数F间有关系有著名的欧拉公式:V-E+F=2. 设G为任意的连通的平面图,则v-e+f=2,v是G的顶点数,e是G的边数,f是G的面数.(引) 证明(?) ...
- Kali linux 2016.2(Rolling)里的应用更新和配置额外安全工具
写在前面的话 你去打人家 ,你不伪装一下,化化妆 ,穿上盔甲,难道你傻逼一样的 拿着棍子就去打人家,人家 一眼不认出你是谁了.做坏事要伪装好自己 ,要把自己藏起来 ,让别人找不到你,你以为网络公 ...
- VS打包后生成快捷方式:目标指向错误、Icon图标分辨率有误问题解决方案
1.目标指向错误: 在安装***.msi文件后,对快捷方式-->右键-->属性: 发现目标并非指exe文件. 于是我新建了一个快捷方式,将目标-->指向exe文件,位置Ctrl+v. ...
- 2017广东工业大学程序设计竞赛决赛 H tmk买礼物
题意: Description 今天是校赛的日子,为了庆祝这么喜庆的日子,TMK打算买些礼物给女票LSH庆祝一下. TMK进入了雪梨超市,然后刚踏入的一瞬间,店主就对TMK说:“恭喜你成为了本店第21 ...
- 关于docker入门教程
简介:docker入门教程 docker入门教程翻译自docker官方网站的Docker getting started 教程,官方网站:https://docs.docker.com/linux/s ...
- http升级https改造方案
一.解决方案 1.httpClient请求https版蓝鲸接口 (1).原理 https与http最大的区别在于SSL加密传输协议的使用.在自己写的JAVA HttpClient程序,想手动验证证书, ...
- H1B工作签证·绿卡:美国留学的两个关键步骤
月20日在留美学生家长群聚会上的发言稿一.H1B签证系美国最主要的工作签证类别,发放给美国公司雇佣的外国籍有专业技能的员工,属于非移民签证的一种.持有H1B签证者可以在美国工作三年,然后可以再延长三年 ...
- 番茄花园Ghost Win10系统X64位10041装机版2015年4月
转载:系统妈,系统下载地址:http://www.xitongma.com/windows10/2015-04-01/6639.html 番茄花园Ghost Win10系统X64位10041装机版20 ...
- ubuntu15.04安装 RVM
首先,请参考这篇文章 https://ruby-china.org/wiki/rvm-guide RVM 官方网站 https://rvm.io/ 1 由于现在很多网站都转向https链接,所以,根据 ...