zoukankan      html  css  js  c++  java
  • 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);
        }
    };
    
  • 相关阅读:
    2020.12.15
    2020.12.14
    2020.12.13
    2020.12.11
    2020.12.10
    语音合成标记语言(SSML)
    Skyline查询
    win10 VMware 安装 Linux 虚拟机
    图像梯度计算
    Harris Corner Detection
  • 原文地址:https://www.cnblogs.com/y119777/p/5840530.html
Copyright © 2011-2022 走看看