码迷,mamicode.com
首页 > 其他好文 > 详细

30.Substring with Concatenation of All Words

时间:2019-04-08 21:25:48      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:order   pre   ack   find   ++i   turn   str   code   ring   

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> res;
        if (s.empty() || words.empty()) return res;
        int n = words.size(), m = words[0].size();
        unordered_map<string, int> m1;
        for (auto &a : words) ++m1[a];
        for (int i = 0; i <= (int)s.size() - n * m; ++i) {
            unordered_map<string, int> m2;
            int j = 0; 
            for (j = 0; j < n; ++j) {
                string t = s.substr(i + j * m, m);
                if (m1.find(t) == m1.end()) break;
                ++m2[t];
                if (m2[t] > m1[t]) break;
            }
            if (j == n) res.push_back(i);
        }
        return res;
    }
};

30.Substring with Concatenation of All Words

标签:order   pre   ack   find   ++i   turn   str   code   ring   

原文地址:https://www.cnblogs.com/smallredness/p/10673295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!