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 ...
随机推荐
- margin双边距的问题
margin:20px;height:20px;float:left margin:20px;height:20px;float:left
- 简单的STM32 汇编程序—闪烁LED
要移植操作系统,汇编是道不得不跨过去的坎.所以承接上篇的思路,我准备用汇编写一个简单的闪烁LED灯的程式.以此练习汇编,为操作系统做准备. 第一步,还是和上篇一样,建立一个空的文件夹. 第二步,因为是 ...
- web在线打印,打印阅览,打印维护,打印设计
winform打印的方案比较多,实现也比较容易,而且效果也非常炫:但现在越来越多的系统是web系统,甚至是移动端.网上也有非常的web打印方案,但各式各样的问题非常多,比如js兼容性,稳定性等一直缠绕 ...
- yaf将错误输出打印在页面上
修改项目的配置文件 文件是conf/application.ini 添加两行代码 application.dispatcher.throwException = 1 ;开启/关闭自动异常捕获功能 ap ...
- [Operate System & Algorithm] 页面置换算法
页面置换算法是什么?我们看一下百度百科对页面置换算法给出的定义:在地址映射过程中,若在页面中发现所要访问的页面不在内存中,则产生缺页中断.当发生缺页中断时,如果操作系统内存中没有空闲页面,则操作系统必 ...
- 如何学习JavaScript
Javascript是我大学里面做网站兴趣,加上进一年维护公司javascript相关的框架. 顺便回顾一下自己学习 javascript 的相关方法和技巧,分享给需要的朋友. 1.base 基础.兼 ...
- Js中文排序(拼音首字母)
演示地址:http://lar5.sinaapp.com/ 1.index.html <html xmlns="http://www.w3.org/1999/xhtml"&g ...
- 获取PHP文件绝对地址$_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别
通常情况下,PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 都会返回 PHP 文件的完整路径(绝对路径)与文件名: <?php echo 'SCRIPT_F ...
- PhpStorm 2016.3 For Mac 重大里程碑更新 -- 终于解决了不能输入中文标点符号的重大bug
PhpStorm 2016.3 For Mac 重大里程碑更新 1.[终于解决了]不能输入中文标点符号的重大bug,如 逗号“,”.“.”: 2.可以在一个窗体中,同时打开多个项目: 3.其他... ...
- Swift3.0P1 语法指南——类和结构体
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...