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. CLion 2016.1.1 下载 附注册激活码 破解版方法

    http://www.520xiazai.com/soft/CLion-2016.1.1.html CLion 2016.1.1 下载 附注册激活码 破解版方法 注册破解方法:在要求输入注册的界面选择 ...

  2. postgresql是如何处理死连接(转)

    在数据库postgresql中,一个客户端到服务器连接实际上是一个tcp socket连接,tcp连接是虚连接,一方非正常退出(如断电),另一方会继续维持这个连接.   举个例子,一个客户端电脑正常连 ...

  3. tyvj1938 最优战舰

    描述 太空战队顺利地完成了它的第一次使命,这一行动的受益者陆军本部当即决定,请陆军的战士们投票选出最优战舰并报司令总部进行表彰.为防止有人利用高科技手段造假,陆军本部决定使用最原始的方法进行投票.可不 ...

  4. bzoj1003 物流运输

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  5. linux下实现在程序运行时的函数替换(热补丁)

    声明:以下的代码成果,是参考了网上的injso技术,在本文的最后会给出地址,同时非常感谢injso技术原作者的分享. 但是injso文章中的代码存在一些问题,所以后面出现的代码是经过作者修改和检测的. ...

  6. svn清除已保存的用户名和密码

    在项目中使用SVN是必须的,我们一般将用户名和密码进行保存处理,这样做的好处在于每次都不用输入了,方便快捷.但是当我们想用另外一个svn账号时,这时候该怎么办呢,看下图,让提示框重新出来. 找到这个页 ...

  7. C和指针 第十一章 习题

    1编写calloc,内部使用malloc函数获取内存 #include <stdio.h> #include <stdlib.h> void *myAlloc(unsigned ...

  8. zozoui

    http://zozoui.com/tabs/?utm_source=cc_tabs&utm_medium=codecanyon&utm_campaign=Codecanyon+-+T ...

  9. C++基础知识(4)---例外、异常处理

    对Java熟悉的朋友们都很清楚,java中的异常处理机制是非常完善的.并且java强制使用异常处理,用户必须对有可能出现异常的情况进行处理. 在C++中并没有强制用户使用异常处理,但是使用异常处理将会 ...

  10. python写计算器

    #!/usr/bin/env python # -*- coding:utf-8 -*- import re def chu(arg1): #定义加减 arg = arg1[0] #beacuse p ...