首页 > 其他 > 详细

leetcode-----30. 串联所有单词的子串

时间:2020-06-20 16:49:50      阅读:61      评论:0      收藏:0      [点我收藏+]

代码

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> ans;
        if (words.empty()) return ans;
        int n = s.size(), m = words.size(), w= words[0].size();

        unordered_map<string, int> map;
        for (auto word: words) map[word]++;

        for (int i = 0; i < w; ++i) {
            unordered_map<string, int> wd;
            int cnt = 0;
            for (int j = i; j + w <= n; j += w) {
                if (j >= i + m * w) {
                    auto word = s.substr(j - m * w, w);
                    wd[word]--;
                    if (wd[word] < map[word]) cnt--;
                }
                auto word = s.substr(j, w);
                wd[word]++;
                if (wd[word] <= map[word]) cnt++;
                if (cnt == m) ans.push_back(j - (m - 1) * w);
            }
        }
        return ans;
    }
};

leetcode-----30. 串联所有单词的子串

原文:https://www.cnblogs.com/clown9804/p/13169048.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!