`
cozilla
  • 浏览: 89437 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[Leetcode] Substring with Concatenation of All Words

 
阅读更多
Substring with Concatenation of All WordsFeb 24 '125071 / 18608

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.

For example, given:
S"barfoothefoobarman"
L["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

 

注:未通过所有case

“aaa”, [a, b],这个case我自己返回是空,但是在leetcode上返回(0,1)。让我很郁闷。

假如unordered_map可以算作是O(1)的话,则这个算法应该是O(n),n = S.size();

 

class Solution {
public:
    unordered_map<string, int> dic;
    vector<int> t;
    void load(const string& s, const vector<string> &l) {
        int n = l.size();
        int cnt = 0;
        for (int i = 0; i < n; ++i)
        {
            auto it = dic.find(l[i]);
            if (it == dic.end()) {
                dic[l[i]] = cnt++;
                t.push_back(1);
            }
            else {
                t[it->second]++;
            }
        }
    }
    vector<int> findSubstring(string S, vector<string> &L) {
        vector<int> res;

        int n = L.size();
        int len = S.size();
        int K = L[0].size();
        load(S, L);

        vector<int> v(len);
        for (int i = 0; i < len - K; ++i)
        {
            auto it = dic.find(S.substr(i, K));
            if (it == dic.end()) v[i] = -1;
            else v[i] = it->second;
        }

        int total = 0, last = len - K * n;
        vector<bool> mk(len, false);
        res.clear();
        for (int i = 0; i <= last; ++i)
        {
            if (mk[i]) continue;
            int start = i, end, cur;
            while (start <= last && dic.find(S.substr(start, K)) == dic.end())
                mk[start] = true,start += K;
            if (start > last) continue;

            if (n == 1) {
                res.push_back(start);
                mk[start] = true;
                continue;
            }

            end = start + K;
            vector<int> cnt(t);
            total = 1;
            auto it = dic.find(S.substr(start, K));
            cnt[it->second]--;
            while (end < len) {
                auto ie = dic.find(S.substr(end, K));
                if (ie == dic.end()) {
                    while (start <= end) mk[start] = true, start += K;
                    break;
                }
                else {
                    cur = ie->second;
                    if (cnt[cur] == 0) {
                        while (start <= end) {
                            mk[start] = true;
                            cnt[v[start]]++;
                            total--;
                            if (cur == v[start]) break;
                            start += K;
                        }
                    }
                    cnt[cur]--;
                    total ++;
                    
                    if (total == n) {
                        res.push_back(start);
                        cnt[v[start]]++;
                        total--;
                        mk[start]=true;
                        start += K;
                    }
                    end += K;
                }
            }
        }

        return res;
    }
};

 

 

0
7
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics