30. Substring with Concatenation of All Words (HashTable)

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[0].length();
        wordsLen = wLen * size;
        for(i = 0; i < size; i++){
            word_counter[words[i]]++;
        }

        i = 0;
        while(i+wordsLen<=sLen){
            for(j = 0; j < size; j++){
                cmpStr = s.substr(i+j*wLen, wLen);
                if(word_counter.find(cmpStr)==word_counter.end()){
                    break;
                }

                counting[cmpStr]++;
                if(counting[cmpStr]>word_counter[cmpStr]){
                    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;
};
时间: 2024-08-04 13:43:56

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

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

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 30: Substring with Concatenation of All Wordshttps://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, S, and a list of words, L, that are all of the

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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, giv

[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 starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters. For example, give

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, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without an

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, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without an

[*leetcode 30] Substring with Concatenation of All Words

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: "b

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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, giv

【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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, giv

19.1.30 [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 starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. Example 1: Input