Leetcode 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).
题目意思: 输入字符串S和列表L,L含有一组长度相同的单词。找出所有子串的开始下标,该子串由L的所有单词拼接而成,而且没有夹杂其他字符
解题思路是:
从原串S的第一个字符开始,取长度为L的元素个数乘以L里单词的长度,然后判断该串是否仅仅含了L得所有单词。
将子串拆分成多个长度和L单词长度一致的单词,然后根据hash_map来匹配子串的单词
如果单词匹配成功,则匹配数加1;
如果最后的匹配数等同于L的元素的个数,那么该串包含了L得所有单词,而且把该串的开始位置放入结果集中,继续考察S的下一个字符开始的字串情况。
下面代码不知为什么会超时
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
vector<int> res;
int l_len = L.size(), sub_l_len = L[].length();
int subLen = l_len * sub_l_len;
unordered_map<string, int> hash_map;
for(int i = ; i < l_len; ++ i ) hash_map[L[i]]++;
for(int i = ; i < S.length()-subLen+; ++i){
string sub = S.substr(i,subLen);
int cnt = ;
unordered_map<string,int> cpHashMap(hash_map);
for(int j = ; j < l_len; ++ j){
string word = sub.substr(j*sub_l_len,sub_l_len);
if(cpHashMap.find(word)==cpHashMap.end() || cpHashMap[word] == ) break;
else{
cpHashMap[word]--;
cnt++;
}
}
if(cnt == l_len) res.push_back(i);
}
return res;
}
};
对上面代码进行优化,减少了hash_map的拷贝
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
vector<int> res;
int l_len = L.size(), sub_l_len = L[].length();
int subLen = l_len * sub_l_len;
unordered_map<string, int> hash_map,curHashMap;
for(int i = ; i < l_len; ++ i ) hash_map[L[i]]++;
for(int i = ; i < S.length()-subLen+; ++i){
string sub = S.substr(i,subLen);
int cnt = ;
curHashMap.clear();
for(int j = ; j < l_len; ++ j){
string word = sub.substr(j*sub_l_len,sub_l_len);
if(hash_map.find(word) ==hash_map.end()) break;
curHashMap[word]++;
if(curHashMap[word] > hash_map[word]) break;
cnt++;
}
if(cnt == l_len) res.push_back(i);
}
return res;
}
};
Leetcode Substring with Concatenation of All Words的更多相关文章
- 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] 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 (summarize)
题目链接 You are given a string, S, and a list of words, L, that are all of the same length. Find all st ...
- [leetcode]Substring with Concatenation of All Words @ Python
原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 题意: You are given a ...
- [LeetCode] Substring with Concatenation of All Words(good)
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Leetcode:Substring with Concatenation of All Words分析和实现
题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...
- LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!
超时代码 class Solution { public: vector<int> findSubstring(string s, vector<string>& wo ...
- 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面试准备: Substring with Concatenation of All Words
leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...
随机推荐
- arcgis desktop 10.1 license manager无法启动问题解决
19:44:36 (ARCGIS) Vendor daemon can't talk to lmgrd (License server machine is down or not respondin ...
- 机器学习中的相似性度量(Similarity Measurement)
机器学习中的相似性度量(Similarity Measurement) 在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间 ...
- JAVA起名规范
1:包名 package com.cenzhongman.模块名.组件 必须全部小写,作为java文件第一行代码 2:类名 名词,表示一类实物,如:人类 首字母大写 3.接口名 形容词/副词,表示一种 ...
- NOSDK--一键打包的实现(二)
Android.mk文件,位置在android工程/jni目录下,是android工程中的makefile文件,这里我们简称它为mk文件. 1.2 自动刷新mk文件的脚本介绍 这一节介绍mk文件的自动 ...
- mysql 主从master-slave同步复制 配置,为读写分离准备
1.为方便,我在一个windows下安装两个mysql实例,端口分别是 3306.3307 打开 my.ini 或 my-default.ini 文件 配置 basedir datadir 和port ...
- mac 上的 python
1.mac 上的 python 自己感觉很乱 1.额外安装的 自带的 python27-apple /System/Library/Frameworks/Python.framework/Versio ...
- 解决 PhpStorm 对 用单例模式实例化PHP类时,代码自动提示功能失效 的问题
大部分PHP框架中,为了防止一个类被重复实例化,往往采用“单例模式”实例化类.我们的项目框架是这样做的: 先写好一个基类 /framework/Base.class.php,内容如下: <?ph ...
- Linux C 中 fork() 函数详解
一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork() 函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同 ...
- Swift3.0P1 语法指南——类和结构体
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- BZOJ1562——[NOI2009]变换序列
1.题意:题意有些难理解 2.分析:我们发现如果要求判断是否合法的话就so easy了,二分图匹配即可,但是我们发现要求输出字典序最小的,那么我们在匈牙利的时候就倒着枚举,另外邻接表中的边一定要排好序 ...