395. Longest Substring with At Least K Repeating Characters

395. Longest Substring with At Least K Repeating Characters
我的思路是先扫描一遍,然后判断是否都满足,否则,不满足的字符一定不出现,可以作为分割符,然后不断切割,重复这个过程,我不知道运行时间,感觉会tle,然后交了,就ac了。

class Solution {
public:
        int work(string &s, int k, int x, int y) {
        if(y - x + 1 < k) return 0;
        set<char> se;
        vector<int> sum(26, 0);
        for (int i = x; i <= y; i++) {
            sum[s[i] - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if(sum[i] && sum[i] < k) {
                se.insert(char('a' + i));
                //cout << x << " " << y << " " << char('a' + i) << endl;
            }
        }
        if(se.size() == 0) {
             return y - x + 1;
        }
        int last = x;
        int res = 0;
        for (int i = x; i <= y; i++) {
            if(se.count(s[i])) {
                    //cout << char(s[i]) << endl;
            //cout << last << " " << i << endl;
            if(i - 1 - last + 1 >= k) {
                int t = work(s, k, last, i - 1);
                res = max(res, t);
            }
                last = i + 1;
            }
        }
        if(!se.count(s[y])) {
            res = max(res, work(s, k, last, y));
        }
        return res;
     }
     int longestSubstring(string s, int k) {
            return work(s, k, 0, s.size() - 1);
    }
};
原文地址:https://www.cnblogs.com/y119777/p/5840530.html