Leetcode——30. Substring with Concatenation of All Words

子串查找算法
本文介绍了一种高效的子串查找算法,旨在找出字符串s中由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, given:
s: “barfoothefoobarman”
words: [“foo”, “bar”]

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

解答

思路:定义两个map容器,结构都是

unordered_map<string,int> counts;

key是字符串,value是该字符串出现的个数。
先对words进行遍历,初始化counts
那下面就要遍历s了,但是我们没必要遍历到最后,因为要匹配整个words,所以下标只需到s_len-words_num*words1_len
这个很重要,许多超时的都是遍历到最后了。
之后用另一个unordered_map (seen)来计数

unordered_map<string,int> seen;

假设某次遍历,开始下标是 i
对于words出现的每一个word,进行遍历,用j表示words中第j个word,并截取s中以i下标开始的字符串word,查看counts中是否包含这个字符串,如果包含,seen[word]就自增,否则(不包含),就是以i开始的字符串不满足,i++。
在上述过程中,seen[word]如果超过counts[word],即出现的次数多了,肯定也不满足,直接跳转到i+1。
当所有的seen[word]都不超过counts[word],这个表示seen中每个字符串的个数等于counts中字符串个数。把此时的下标i保存起来。

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
    unordered_map<string,int> counts;
    for(int i=0;i<words.size();i++)
    {
        counts[words[i]]++;//初始化,每个string的个数
    }

    int s_len=s.length(),words_num=words.size(),words1_len=words[0].size();
    vector<int> indexs;
    for(int i=0;i<s_len-words_num*words1_len+1;i++)//遍历字符串中的字符,只需要遍历到s_len-words_len*words1_len+1即可
    {
        unordered_map<string,int> seen;
        int j=0;
        for(;j<words_num;j++)
        {
            string word=s.substr(i+j*words1_len,words1_len);//遍历s中每个words1_len长的字符串
            if(counts.find(word)!=counts.end())
            {
                seen[word]++;//用于计数
                if(seen[word]>counts[word])//对应的word超过个数了
                    break;
            }
            else//不包含word
                break;
        }
        if(j==words_num) //比较的个数达到words_num个,满足条件
            indexs.push_back(i);
    }

    return indexs;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值