You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.

For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

思路: 判断一个值是否包含在一个数组中,首先应该想到将这个数组中的元素放入HashTable,否则每次查找都需要O(n)的时间复杂度。

时间复杂度:O(n*size),其中n为s的长度,size是指数组words包含多少个元素。当words元素不多的时候,我们可以说时间复杂度是线性的O(n)

class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
size = words.size();
sLen = s.length();
wLen = words[].length();
wordsLen = wLen * size;
for(i = ; i < size; i++){
word_counter[words[i]]++;
} i = ;
while(i+wordsLen<=sLen){
for(j = ; j < size; j++){
cmpStr = s.substr(i+j*wLen, wLen);
if(word_counter.find(cmpStr)==word_counter.end()){ //不在words中,不符合
break;
} counting[cmpStr]++;
if(counting[cmpStr]>word_counter[cmpStr]){ //出现的次数多过words中的次数,不符合
break;
}
}
if(j==size){//找到了一个符合的结果
ret.push_back(i);
}
counting.clear();
i++;
}
return ret;
}
private:
string cmpStr;
vector<int> ret;
map<string,int> word_counter;
map<string,int> counting;
int size; //number of words
int sLen;
int wLen;
int wordsLen;
int i;
int j;
};

30. Substring with Concatenation of All Words (String; HashTable)的更多相关文章

  1. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  2. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  3. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  4. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

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

  6. [LeetCode] 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 ...

  7. Java [leetcode 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 a ...

  8. 【LeetCode】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】#30. Substring with Concatenation of All Words

    注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...

随机推荐

  1. L203 词汇题

    Conditions for the growth of this plant are optimum in early summer.we will live as free people, not ...

  2. C# ,asp.net 获取当前,相对,绝对路径

    一.C#获取当前路径的方法: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. 2. ...

  3. SendMessage wMsg常量值参考

    namespace Core.WinAPI { /// <summary> /// wMsg参数常量值: /// </summary> public static class ...

  4. Linux运维学习笔记-角色知识总结

    角色通过UID和GID区分 root:超级管理员,拥有所有权限,UID(0). 普通用户:拥有操作自己家目录下的所有权限,其他文件及目录(/etc./var)只有读的权限,UID(500-65535) ...

  5. ZetCode PyQt4 tutorial basic painting

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  6. .NET/C# 使用反射注册事件

    使用反射,我们可以很容易地在运行时调用一些编译时无法确定的属性.方法等.那么如何注册事件呢? 本文将介绍如何使用反射注册事件. 本文内容 不使用反射 使用反射 安全地使用反射 参考资料 不使用反射 例 ...

  7. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

  8. Linux共享对象之编译参数fPIC(转)

    最近在看Linux编程的基础知识,打算对一些比较有趣的知识做一些汇总备忘,本文围绕fPIC展开,学习参考见文末. 在Linux系统中,动态链接文件称为动态共享对象(DSO,Dynamic Shared ...

  9. volatile的本质

    1. 编译器的优化 在本次线程内, 当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后,再取变量值时,就直接从寄存器中取值:当变量值在本线程里改变时,会同时把变量的新 ...

  10. linux 使用中括号进行条件判断

       格式 “#”代表空格,不可缺少 [# param1#op# param2 #] 这种带比较操作符的形式,op左右必须使用空格隔开. 如 [# “3”==”2” #]  这种缺少空格的写法会得到结 ...