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. Alpha冲刺一 (6/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10004107.html 作业博客:https://edu.cnblogs.com/campus ...

  2. 转:application/json 四种常见的 POST 提交数据方式

    四种常见的 POST 提交数据方式 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 PO ...

  3. hibernate映射对象三种状态的分析

    一,首先hibernate中对象的状态有 三种:瞬态.游离态和持久态,三种状态转化的方法都是通过session来调用,瞬态到持久态的方法有save().saveOrUpdate(). get().lo ...

  4. 如何查看 ThinkPHP5.1 的升级说明

    如何查看 ThinkPHP5.1 的升级说明 ThinkPHP 官方对于升级历史都有说明,这个官方做的非常不错. 在官方的手册中就有. 比如从 ThinkPHP 5.1.26 升级到 ThinkPHP ...

  5. bzoj2330糖果

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束裸题.练习用spfa判正环(一个点入队超过n次). 据说有1e5个点连成一条链 ...

  6. ProcessHelp 进程类(启动,杀掉,查找)

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  7. WCF揭秘学习笔记(3):使用DataContractSerializer

    使用DataContractSerializer 终结点(包括地址.绑定.契约)可通过代码以编程方式添加到服务中.如: using(ServiceHost host =new ServiceHost( ...

  8. emacs配置buffer的快捷键

    emacsConfig/buffer-setting.el (global-set-key (kbd "<M-left>") 'previous-buffer) (gl ...

  9. emacs之配置php

    php-setting.el (require 'php-mode) 以后丰富

  10. jsp中遇到Integer的方法valueOf()和parseInt()的区别.前者要求是对象类型,后者是数字型字符串

    他们有本质区别,Integer.valueof(String s)是将一个包装类是将一个实际值为数字的变量先转成string型再将它转成Integer型的包装类对象(相当于转成了int的对象)这样转完 ...