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. 收藏一些好用的fifo

    1.Nordic库中的 E:\nRF52_SDK_0.9.2_dbc28c9\components\libraries\fifo app_fifo.c /* Copyright (c) 2013 No ...

  2. Android锁屏后数据改变的解决方案

    如果一个界面设置成横屏,那么锁屏再开启之后,会重新执行一遍onCreate()方法.对于这个问题的解决方案如下: 只需要在Menifest文件的activity相应标签下添加这行代码即可: andro ...

  3. 墙裂推荐一本案例驱动的PhoneGap入门书,早看早收货

    清华大学出版社推出的<构建跨平台APP:PhoneGap移动应用实战> 零门槛学APP开发 从无到有 循序渐进 20余个示例APP 3个项目APP 全平台à跨终端à移动开发 完美生命周期: ...

  4. UI第十五节——UIWebView

    - (void)viewDidLoad {    [super viewDidLoad];    UIWebView *webView = [[UIWebView alloc] initWithFra ...

  5. CentOS6.3 编译安装LAMP(4):编译安装 PHP5.3.27

    所需源码包: /usr/local/src/PHP-5.3.27/libmcrypt-2.5.8.tar.gz /usr/local/src/PHP-5.3.27/mhash-0.9.9.9.tar. ...

  6. phpcms专题路径修改

    两个文件改三处就可以了,既可以后台点击专题列表链接问题,也可以解决生成专题多一个“/”的问题. 1.\phpcms\modules\special\classes\html.class.php大概第1 ...

  7. 第七届山东省ACM省赛

    激动人心的省赛终于结束了…平静下来再回头看真的感觉一波三折…先是赛前毫无预兆的查出突发性耳聋…伴随而来的就是左耳听力下降.轻微耳鸣.极个别情况下的头晕…不过这都还好,毕竟药物可以恢复…热身赛只过了一道 ...

  8. js 制作MD5加密

    主要使用已经写好的JS插件,由于网上有很多,同时自己也可根据原理写出,但为了加快开发速度,我选择了能使用的,写得还不错的js http://pajhome.org.uk/crypt/md5/md5.h ...

  9. 无需activity获得屏幕尺寸

    原文地址:http://blog.sina.com.cn/s/blog_4c277ad30100yfqo.html 客户需求需要增加对手机尺寸的读取 于是找了两个方法 第一种:通过activity引用 ...

  10. 用 nssm 把 Nginx 安装成 Windows 服务方法

    总之:用 nssm 比 srvany.exe 简便多了.1. 下载nginx windows版本:http://nginx.org/ 2. 下载 nssm :http://nssm.cc/3. 安装N ...