LeetCode 030 Substring with Concatenation of All Words
题目要求: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 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).
分析:
参考网址:http://www.cnblogs.com/panda_lin/archive/2013/10/30/substring_with_concatenation_of_all_words.html
代码如下:
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
int l_size = L.size();
if (l_size <= 0) {
return vector<int>();
}
vector<int> result;
map<string, int> word_count;
int word_size = L[0].size();
int i, j;
for (i = 0; i < l_size; ++i) {
++word_count[L[i]];
}
map<string, int> counting;
for (i = 0; i <= (int)S.length() - (l_size * word_size); ++i) {
counting.clear();
for (j = 0; j < l_size; ++j) {
string word = S.substr(i + j * word_size, word_size);
if (word_count.find(word) != word_count.end()) {
++counting[word];
if (counting[word] > word_count[word]) {
break;
}
}
else {
break;
}
}
if (j == l_size) {
result.push_back(i);
}
}
return result;
}
};
LeetCode 030 Substring with Concatenation of All Words的更多相关文章
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- [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 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
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- leetcode python 030 Substring with Concatenation of All Words
## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...
- [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 sta ...
- 【leetcode】Substring with Concatenation of All Words (hard) ★
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Java [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 a ...
随机推荐
- Java学习的第九天
1.双色球联系: 对象的创建和使用 2.静态没太明白. 3.明天学习java 的方法
- 最简单的基于FFmpeg的直播系统开发移动端例子:IOS 视频解码器
本文记录IOS平台下基于FFmpeg的视频解码器.该示例C语言的源代码来自于<最简单的基于FFMPEG+SDL的视频播放器>.相关的概念就不再重复记录了. 源代码 项目的目录结构如图所示. ...
- c# sqlhlpear
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- C# 集合类(三)
C# 集合类自己经常用到: 数组(Array).动态数组(ArrayList).列表(List).哈希表(Hashtable).字典(Dictionary),对于经常使用的这些数据结构,做一个总结,便 ...
- 使用git处理github中提交有冲突的pull request
前言: 为什么要写这篇文章,因为前段时间有一个开源的github中的项目有一个朋友提交了一个pr看了下是帮忙优化了下代码(十分感谢这位网友).但是他提交的pr刚好和我的项目有许多的冲突导致无法自动合并 ...
- ORA-00020: maximum number of processes (40) exceeded模拟会话连接数满
问题描述:在正式生产环境中,有的库建的process和session连接数目设置的较小,导致后期满了无法连接.因为正式库无法进行停库修改,只能释放连接,做个测试模拟 1. 修改现有最大会话与进程连接数 ...
- 【python】将变量保存在本地及读取
在用jupyter notebook写python代码的过程中会产生很多变量,而关闭后或者restart jupyter kernel后所有变量均会消失,想要查看变量就必须将代码重新再运行一遍,而想在 ...
- CC模型加载太慢?一招破解!
伴随无人机性能的提升,单个项目涉及到的倾斜摄影数据范围不断扩大,模型的数据量越来越大,在同配置机器上的显示速度也相应的越来越慢,那么如何在不升级配置的情况下提升模型的加载速度呢? 01 百GB倾斜摄影 ...
- yum针对软件包操作的常用命令
yum针对软件包操作的常用命令: 1.使用YUM查找软件包 命令:yum search php 2.列出所有可安装的软件包 命令:yum list php 3.列出所有可更新的软件包 命令:yum l ...
- 第05组 Alpha冲刺(4/6)
.th1 { font-family: 黑体; font-size: 25px; color: rgba(0, 0, 255, 1) } #ka { margin-top: 50px } .aaa11 ...