[LeetCode] (medium) 378. Kth Smallest Element in a Sorted Matrix395. Longest Substring with At Least

https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

Example 1:

Input:
s = "aaabb", k = 3

Output:
3

The longest substring is "aaa", as 'a' is repeated 3 times.

 

Example 2:

Input:
s = "ababbc", k = 2

Output:
5

The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.

这篇博客说的很好, 寻找最大字串的问题一般往三种上去想,分别是dp、双指针、分治。

分治的难点就在于寻找合适的划分点和两段归并时跨立字符串的处理,这题里只要找到“在当前活跃范围内总数小于k的字符”即可,因为包含这个字符的字符串必然不满足条件,所以若以该字符为划分点则不需要处理跨立的可能性。

class Solution {
public:
    int longestSubstring(string s, int k) {
        return longestSubstringInRange(s, k, 0, s.size()-1);
    }
    
    int longestSubstringInRange(string &s, int k, int st, int ed){
        if(st > ed) return 0;
        int cnt[26] = {0};
        for(int i = st; i <= ed; ++i){
            ++cnt[s[i]-'a'];
        }
        int cur;
        for(cur = 0; cur < 26; ++cur){
            if(cnt[cur] > 0 && cnt[cur] < k) break;
        }
        // for(cur = st; cur <= ed; ++cur){
        //     if(cnt[s[cur]-'a'] < k) break;
        // }
        if(cur >= 26){
            return ed-st+1;
        }else{
            // cout << (char)('a'+cur) << ' ';
            cur = s.find((char)('a'+cur), st);
            // cout << cur << endl;
            return max(longestSubstringInRange(s, k, st, cur-1), longestSubstringInRange(s, k, cur+1, ed));
        }
    }
};

但是这个程序其实效率很低,原因如下:

1、每次都只是二分,其实一次遍历可以找到多个划分点,可以直接进行多次划分,既可以减少递归次数,又可以减少反复寻找下标的开销

2、递归终止条件过于宽松

修改之后程序如下:

class Solution {
public:
    int longestSubstring(string s, int k) {
        static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
        return dfs(s, k, 0, s.size()-1);
    }
    
    int dfs(string &s, int k, int st, int ed){
        if(ed-st+1 < k) return 0;   //提前终止递归
        int cnt[26] = {0};
        for(int i = st; i <= ed; ++i){
            ++cnt[s[i]-'a'];
        }
        int pre = st;
        int result = 0;
        for(int i = st; i <= ed; ++i){
            if(cnt[s[i]-'a'] > 0 && cnt[s[i]-'a'] < k){
                result = max(result, dfs(s, k, pre, i-1));
                pre = i+1;
            }
        }
        if(pre == st) return ed-st+1;
        else return max(result, dfs(s, k, pre, ed));
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值