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 ...
随机推荐
- 关于apply、call和bind的总结
基础知识不是你看了一遍书或者两篇文章就能掌握的. 之前看书看文章时,感觉自己看懂了就掌握了.呵呵!too young!too naive! 以前的坑还是要一铲一铲的填上的. 高程上面关于apply和c ...
- 三十分钟理解:双调排序Bitonic Sort,适合并行计算的排序算法
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入 双调排序是data-indepen ...
- 前端 —— SVG
0. 简介 SVG:可缩放矢量图形: SVG 是代码,通过浏览器的解析而渲染成一种图形: 可缩放矢量图形是基于可扩展标记语言(XML),以描述二维矢量图形的一种图形格式,由万维网联盟( World W ...
- Redis3.0集群
Redis集群介绍 Redis 集群是一个提供在多个Redis间节点间共享数据的程序集. Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性 ...
- 6-3 Add Two Polynomials(20 分)
Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation ...
- Oracle单表去重复(一)
去重有两层含义,一:是记录完全一样:二:是符合一定条件的认为是重复. 根据表的数量,去重可划分为:单表去重和多表关联去重. 对于去重,一般最容易想到的是用distinct,而distinct只能对 ...
- sysstat工具
sysstat工具可以监控系统的IO,CPU,SWAP,LOAD,NETWORK,DISK 安装后,系统会生成定时任务脚本 路径:/etc/cron.d/sysstat 内容: # Run syste ...
- 解决Apache下生成静态页面乱码的问题
我的空间存放在阿里云,服务器默认Apache编码设置为utf-8,而新的网站珠宝招聘网http://hr.izuans.com 采用GB2312编码,其他程序文件都OK,就是生成静态新闻页和其他单页面 ...
- 【转】MFC消息处理(一)
原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182120 1.MFC窗口如何与AfxWndProc建立联系. 当一个新的CWnd派生 ...
- php 自定义函数大全
1. call_user_func和call_user_func_array 以上两个函数以不同的参数形式调用函数.见如下示例: <?php class demo{ public static ...