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.例8.4找出整形数组中的最大值 import java.util.Scanner; public class Cjava { public static void main(String[]arg ...
- python类的封装与继承
封装 关注公众号"轻松学编程"了解更多. 1.概念 面向对象语言的三大特征:封装, 继承, 多态. 广义的封装: 类和函数的定义本身就是封装的体现. 狭义的封装:一个类的某些属性, ...
- K8S 搭建 Kafka:2.13-2.6.0 和 Zookeeper:3.6.2 集群
搭建 Kafka:2.13-2.6.0 和 Zookeeper:3.6.2 集群 一.服务版本信息: Kafka:v2.13-2.6.0 Zookeeper:v3.6.2 Kubernetes:v1. ...
- 我用 Python 找出了删除我微信的所有人并将他们自动化删除了
1. 概述 不知你是否遇到过在微信上给通讯录中的某个人发消息,结果出现了这一幕: 平时一直认为自己的心里素质过硬,不过遇到这种情况 ... 在我缓了半个钟头(半分钟)之后,缓缓拿出了手机,打开微信,找 ...
- js 操作css
类似于jquery的css()函数,js封装 CSS函数:css(oDiv , "width" , "200px ")设置样式css(oDiv , " ...
- thinkPHPbiji
ThinkPHP3.2验证码操作 在当前的控制器内 我这里是登录后台控制器 class LoginController extends Controller { public function ind ...
- 交换机基于接口划分VLAN(汇聚层设备作为网关)
组网图形 简介 划分VLAN的方式有:基于接口.基于MAC地址.基于IP子网.基于协议.基于策略(MAC地址.IP地址.接口).其中基于接口划分VLAN,是最简单,最常见的划分方式,如接入层设备作为网 ...
- php 获取抖音id
<?php public function getid($dy_url){ $header=get_headers($dy_url); $str = "/^.*?(\d+).*/&qu ...
- Life is Strange 完结感想
Life is Strange 是一款剧情游戏.steam上第一章是免费的,也正是因为这个我才开始了解到这个游戏,玩完第一章后觉得还可以,就趁打折花了17块钱买了2-5章.应该是18年6月20号买的, ...
- waf 引擎 云原生平台tproxy 实现调研
了解了基本 云原生架构,不清楚的查看之前的文章:https://www.cnblogs.com/codestack/p/13914134.html 现在来看看云原生平台tproxy waf引擎串联实现 ...