【leetcode】30.substring-with-concatenation-of-all-words(串联所有单词的子串)

【leetcode】30.substring-with-concatenation-of-all-words(串联所有单词的子串)

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:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:

Input:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
Output: []

30.串联所有单词的子串

给定一个字符串 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"]
输出:[]

思路:

暴力搜索会超时。

思想类似于以前的一题“寻找最长无重复字符字串”:

使用前后指针。

前指针移动,并把字符加入容器中(可以队列或map)。

判断容器中是否有重复字符(新加入的字符造成重复)。

有重复字符时,后指针移动并弹出字符,直到容器中不存在重复字符。

继续如上操作。

  1. 由于每个单词都是等长的,那么只要确定遍历的起点,接下来每个单词开始匹配的位置都是确定的。
  2. 使用 map ms 记录单词的情况(words[i] 为 key,数量为 value)。
  3. 使用 map mt 记录遍历过程中的各个满足条件的单词数量。
  4. 使用 queue q 记录单词的首尾顺序(记录 mt::iterator 即可,方便变更临时容器 mt 的情况)
  5. 遍历读取单词 ss,判断 ms 是否存在该单词。不存在时情况当前容器的状态。
  6. ms 存在单词 ss 时,把数量记录到 mt 和 q 中。
  7. 如果当前 mt[ss] > ms[ss] ,这时就需要 q 弹出前面的单词直到 mt[ss] == ms[ss]
  8. 当 q.size() == words.size() 即为一组解。

CODE:

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> ret;
        if (s.empty() || words.size() == 0) return ret;

        map<string, int> ms;
        map<string, int> mt;
        queue<map<string, int>::iterator> q;

        for (int i = 0; i < words.size(); ++i) { ++ms[words[i]]; }
        int wlen = words[0].size();
        for (int j = 0; j < wlen; ++j) {
            { mt.clear(); while(!q.empty()) { q.pop(); } }  // 清空 mt 和 q
            for (int i = j; i+wlen <= s.length(); i+=wlen) {
                string ss = s.substr(i, wlen);
                auto ms_it = ms.find(ss);
                if (ms_it == ms.end()) {
                    { mt.clear(); while(!q.empty()) { q.pop(); } }  // 清空 mt 和 q
                    continue;
                }
                ++mt[ss];
                auto mt_it = mt.find(ss);
                q.push(mt_it);
                if (mt_it->second > ms_it->second) {
                    map<string, int>::iterator it;
                    do {
                        it = q.front();
                        --it->second;
                        q.pop();
                    } while(it != mt_it);
                }

                if (q.size() == words.size()) {
                    ret.push_back(i-wlen*(words.size()-1));
                }
            }
        }

        return ret;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值