zoukankan      html  css  js  c++  java
  • leetcode 30 Substring with Concatenation of All Words

    class Solution {
    public:
        vector<int> findSubstring(string s, vector<string>& words) {
            if (s.empty() || words.empty()) return {};
            vector<int> res;
            int n = s.size(), cnt = words.size(), len = words[0].size();
            unordered_map<string, int> m1;
            for (string w : words) ++m1[w];
            for (int i = 0; i < len; ++i) {
                int left = i, count = 0;
                unordered_map<string, int> m2;
                for (int j = i; j <= n - len; j += len) {
                    string t = s.substr(j, len);
                    if (m1.count(t)) {
                        ++m2[t];
                        if (m2[t] <= m1[t]) {
                            ++count;
                        } else {
                            while (m2[t] > m1[t]) {
                                string t1 = s.substr(left, len);
                                --m2[t1];
                                if (m2[t1] < m1[t1]) --count;
                                left += len;
                            }
                        }
                        if (count == cnt) {
                            res.push_back(left);
                            --m2[s.substr(left, len)];
                            --count;
                            left += len;
                        }
                    } else {
                        m2.clear();
                        count = 0;
                        left = j + len;
                    }
                }
            }
            return res;
        }
    };
  • 相关阅读:
    Eclipse常用插件汇总
    关于销售订单
    java下载文件的种方式
    左右对联
    链表
    Spring MVC 入门
    JAVA环境配置总结
    struts2 iterator判断奇偶
    保存页面的浏览记录
    心扬JS分页
  • 原文地址:https://www.cnblogs.com/wangkun1993/p/6390320.html
Copyright © 2011-2022 走看看