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 ...
随机推荐
- Django 之 Ajax
此次主要是做省市区的三级联动. 环境:django 1.10 1. urls.py # coding:utf-8 from django.conf.urls import url import vie ...
- 如何解决VC "应用程序无法启动,因为应用程序的并行配置不正确 sxstrace.exe"问题
引用链接 http://blog.csdn.net/pizi0475/article/details/7790992 应用程序事件日志中: “C:\windows\system32\test.exe” ...
- python3 爬虫相关-requests和BeautifulSoup
前言 时间的关系,这篇文章只记录了相关库的使用,没有进行深入分析,各位看官请见谅(还是因为懒.....) requests使用 发送无参数的get请求 r = requests.get('http:/ ...
- Python源码分析之dis
一.简单例子 def add(a, b): return a + b add_nums.py import foo a = [1, 'python'] a = 'a string' def func( ...
- oracle之 关闭透明大页
方法一: 1.设置/etc/grub.conf文件,添加 transparent_hugepage=never ,在系统启动是禁用 [root@hbdw1 ~]# cat /etc/grub.conf ...
- java的static研究
(1)static关键字:可以用于修饰属性.方法和类. 1,属性:无论一个类生成了多少个对象,所有这些对象共同使用唯一的一份静态的成员变量(不能修饰临时变量 2,方法:static修饰的方法叫做静态, ...
- 什么是HBase(六)性能调优
硬件层面 内存要大,最好是ECC(Error Checking and Correcting),实现内存的动态纠错:CPU要多路(每个路彼此隔离)每个路一个CPU,每个cpu上面一般都是2~12核. ...
- Zabbix 报警通知邮件和微信vim /etc/hosts
1安装 sendmail # yum -y install sendmail echo 'This is test mail'>body.txt mail -s 'Test mail' 3013 ...
- (判断)window.open()窗口被关闭后执行事件
$(function() { // start ready var $article_share=$('#body .article').find('li.share'); // $article_s ...
- codevs 计算器的改良
#include<iostream> #include<cctype> #include<vector> #include<cstdio> using ...