30. Substring with Concatenation of All Words (String; HashTable)
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 wordsexactly 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).
思路: 判断一个值是否包含在一个数组中,首先应该想到将这个数组中的元素放入HashTable,否则每次查找都需要O(n)的时间复杂度。
时间复杂度:O(n*size),其中n为s的长度,size是指数组words包含多少个元素。当words元素不多的时候,我们可以说时间复杂度是线性的O(n)
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
size = words.size();
sLen = s.length();
wLen = words[].length();
wordsLen = wLen * size;
for(i = ; i < size; i++){
word_counter[words[i]]++;
}
i = ;
while(i+wordsLen<=sLen){
for(j = ; j < size; j++){
cmpStr = s.substr(i+j*wLen, wLen);
if(word_counter.find(cmpStr)==word_counter.end()){ //不在words中,不符合
break;
}
counting[cmpStr]++;
if(counting[cmpStr]>word_counter[cmpStr]){ //出现的次数多过words中的次数,不符合
break;
}
}
if(j==size){//找到了一个符合的结果
ret.push_back(i);
}
counting.clear();
i++;
}
return ret;
}
private:
string cmpStr;
vector<int> ret;
map<string,int> word_counter;
map<string,int> counting;
int size; //number of words
int sLen;
int wLen;
int wordsLen;
int i;
int j;
};
30. Substring with Concatenation of All Words (String; HashTable)的更多相关文章
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- [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 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 ...
- [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 ...
- 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 ...
- 【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】#30. Substring with Concatenation of All Words
注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...
随机推荐
- 6.4-6.5 使用form表单验证,完善登录页面
之前是使用自定义的类来实现登录逻辑,现在使用django内置的form表单验证,用继承django的view来实现登录页面. users > views.py 的内容是: from django ...
- SOA实践指南-读书笔记
SOA是英文Service-Oriented Architecture,即面向服务架构的缩写. SOA是一种范式,目的是增强灵活性.SOA很适宜处理复杂的分布式系统. SOA方法接受异质(不同的平台, ...
- (转)基于DDD的现代ASP.NET开发框架--ABP分层架构
介绍DDD概念Eric Evans的“Domain-Driven Design领域驱动设计”简称 DDD,它是一套综合软件系统分析和设计的面向对象建模方法,或者可称为MDD模型驱动方法的一种,区别于M ...
- iOS 关于时间天数星期月份的总结
#import <Foundation/Foundation.h> @interface NSDate (SLExtend) // 判断是否是本周更早 - (BOOL)isThisWeek ...
- CI框架的引导流程以及基准测试类
一[CI]框架的引导流程了解一下,并掌握如何新增自己的扩展类库 http://www.cnblogs.com/ohmygirl/p/CIRead-4.html // CI框架源码阅读笔记4 引导文 ...
- ArrayAdapter的用法
list = new ArrayList<String>(); //创建一个list list.add("ID列表"); //列表里面的条目 /*ArrayAdapte ...
- Spring4源码解析:BeanDefinition架构及实现
一.架构图 首先共同看下总体的 Java Class Diagrams 图: 二.具体类实现 2.1 AttributeAccessor 接口定义了一个通用的可对任意对象获取.修改等操作元数据的附加契 ...
- TVS 选型
TVS 选型 主要用于 IO 保护 TVS 的电压不要小于正常工作的电压,不然会影响正常工作. ESD 的电压很高,但是时间很短. TVS 有节电容,用于高速线路时要注意. 有双向的,也有单向的,还有 ...
- 容器平台选型的十大模式:Docker、DC/OS、K8S谁与当先?
首先我们来谈什么情况下应该使用Docker的问题 如图,左面是经常挂在嘴边的所谓容器的优势,但是虚拟机都能一一怼回去. 如果部署的是一个传统的应用,这个应用启动速度慢,进程数量少,基本不更新,那么 ...
- Java 8 Lambda表达式之方法引用 ::双冒号操作符
双冒号运算符就是java中的方法引用,方法引用的格式是类名::方法名. 这里只是方法名,方法名的后面没有括号“()”.--------> 这样的式子并不代表一定会调用这个方法.这种式子一般是用作 ...