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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. [leetcode]Substring with Concatenation of All Words @ Python

    原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 题意: You are given a ...

  5. [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 ...

  6. Leetcode:Substring with Concatenation of All Words分析和实现

    题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...

  7. LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!

    超时代码 class Solution { public: vector<int> findSubstring(string s, vector<string>& wo ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. IO

    文件过滤 http://codego.net/9245/ C# 文件处理 http://wenku.baidu.com/link?url=yXKiIA_OZYR4MIynDgz-qhOnfJoCyOQ ...

  2. 去掉IE11的叉叉

    在 IE11 下,浏览器自作多情在 text input 组件上加一个 close 叉叉: 用CSS伪类定义: input::-ms-clear { display: none; }

  3. 百度地图API简单应用

    在做移动端应用时经常用到百度地图API,百度API有强大的示例和文档,开发之前去百度相关网站注册密钥,很块博主只花了几分钟 百度地图API范例 百度地图API文档说明 例子1:输入特定关键字绘制地图标 ...

  4. ASP.NET WEB API路由机制

    (一)路由原理 (二)路由设计架构分析 RouteBase

  5. 【重装系统】线上Linux服务器(2TB)分区参考方案

    如果是线上服务器,假设它是 2TB 的 SATA 硬盘.8GB 内存,建议按如下方式进行分区: / 20480M(20G)(主分区) /boot 128M swap 10240M /data 2016 ...

  6. 搭建一个简单struts2框架的登陆

    第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252 出于学习的目的,可以把整个完整的压 ...

  7. 64位操作系统 通过ODP.NET 访问ORACLE 11g

    摘要:64位操作系统部署.NET 程序访问oracle时,无法连接问题.(注意:客户端是64位系统 ,服务端是否64位 还是32位无关.) 1.到oracle 官网搜索相关版本的 ODAC网址: ht ...

  8. How to install Shadow•socks in CentOS7

    Helps from: http://www.cmsky.com/shadowsocks-python-install/ http://shadowsocks.blogspot.jp/?m=1 wge ...

  9. SystemErrorCodes

    有人把SystemErrorCodes整理成了类,并定义了方法,用于返回消息,他大概不知道FormatMessage的用法,放在这里做参考吧 C# code snipppet class System ...

  10. jQuery hover事件

    hover(over,out)一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法.这是一个自定义的方法,它为频繁使用的任务提供了一种"保持在其中"的状态. 当鼠标移动到 ...