Leetcode 30 串联所有单词的子串

Leetcode 30 串联所有单词的子串

我的 leetcode 代码仓库

题目描述

Leetcode题目地址:https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/

给定一个字符串 s 和一些长度相同的单词 words找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

示例 1:

输入:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。

示例 2:

输入:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
输出:[]

解答

使用哈希表以及滑窗处理

  1. 将输入数组words包装为一个名为dict的map降低查找复杂度

  2. 将以字符为单位转为以单词为单位

    比如示例1中的每个单词长度为3,那么只需要从s的0,1,2开始每3个字符遍历,就可以遍历所有的单词

    因为最后的合法串是连续的,所以遍历单词也是连续的,这样就可以遍历所有符合要求的单词

  3. 在每一个单词串遍历中使用滑窗降低复杂度

    开辟一个新的map用来判断滑窗内单词合法性

    以滑窗初始位置left 以及滑窗长度 len 表示一个滑窗(如果长度是一个合法串应有的长度则将left加入到res 中,如果当前单词是非法的(不再dict中)则,重新定义滑窗(left为当前非法位置的下一个,len = 0),否则判断是否出现重复,如果有,则重新定位left(第一个重复单词处)以及应有的len值,否则len++以及更新的map里的值

代码如下

注意:

如果一开始初始化tempDictdict,然后通过每次将tempDict的单词减1,即每次要重新构造map而不是使用map的clear方法,会花大量的时间,尽管思路是一样的,可能是构造的复杂度比clear更高了

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> res;
        if(words.empty() || s.empty() || s.length() < words.size()*words[0].length()) return res;	// 一定不行的情况

        int lenOfWord = words[0].length();    // 记录单词的长度
        unordered_map<string,int> dict; // 存最初的单词以及出现次数

        for(int i = 0;i < words.size();i++) {   // 初始化最初的dict
            if(dict.find(words[i]) == dict.end()) {
                dict.insert(make_pair(words[i],1));
            } else {
                dict[words[i]]++;
            }
        }
        // 以单词位单位进行遍历
        // strBeg: 每个单词串的开头
        for(int strBeg = 0;strBeg < lenOfWord;strBeg++) {   // 每一个单词串开头
            unordered_map<string,int> tempDict;
            int left = strBeg, len = 0;
            for(int cur = strBeg;cur <= s.length()-lenOfWord;cur += lenOfWord) {
                string substr = s.substr(cur,lenOfWord);
                if(dict.count(substr) == 0) {   // 非法单词
                    tempDict.clear();
                    len = 0;
                    left = cur + lenOfWord;
                } else {
                    if(tempDict[substr] < dict[substr]) { // 没有重复
                        tempDict[substr]++;
                        len++;
                    } else { // 有重复
                        string temp;
                        while((temp = s.substr(left,lenOfWord)) != substr) {
                            tempDict[temp]--;
                            left += lenOfWord;
                            len--;
                        }
                        left += lenOfWord;
                    }
                    if(len == words.size()) {	// 此时滑窗内为所要求串
                        res.push_back(left);
                        tempDict[s.substr(left,lenOfWord)]--;
                        len--;
                        left += lenOfWord;
                    }
                }
            }
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_40953281/article/details/86664318