leetcode_[python/C++]_395_Longest Substring with At Least K Repeating Characters

本文介绍了一种解决最长重复子串问题的递归算法,通过实例详细展示了如何找到一个字符串中每个字符至少重复k次的最长子串。该算法巧妙地通过递归分割不满足条件的字符来逐步逼近解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

【题目】

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.


【分析】

这道题写了老半天写不出来,无奈求助网上其他博主的做法,发现大家多是用了一种递归的方法,从起初的一整个字符串,然后在k约束条件下递归搜索不满足条件的字符位置的左边字符串和右边的字符串,从而记录最大值

想一想还是比较巧妙的

比如:

“abbcadda" 2

step 1: str = "abbcadda" 不满足条件的字符为c,因为只有c不满足至少重复2次,所以递归索引左右边字符串"abb" ,”adda"

----------------------------------------------------------------------------左

step 2: str1 = "abb"  不满足条件的字符为a,递归”“ 和”bb"

---------------------左

step 3:str2 = ""

---------------------右

step 4:str3 = "bb" 满足条件,maxlen = 2

----------------------------------------------------------------------------右

step 5:str4 = "adda" 满足条件,maxlen = 4 > 2 

所以maxlen = 4, 即“adda"

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

先说一下递归的方法:

C++:

int longestSubstring(const string &s, int k) {
    return helper(s, 0, s.size(), k);
}
int helper(const string &s, int beg, int end, int k){
    if(end - beg < k) return 0;
    int cnt[26]{};
    for(int i = beg; i < end; ++i) ++cnt[s[i]-'a'];
    for(int i = 0; i < 26; ++i)
        if (cnt[i] && cnt[i] < k)
            for(int j = beg; j < end; ++j)
                if(s[j] == i + 'a')
                    return max(helper(s, beg, j, k), helper(s, j + 1, end, k));
    return end - beg;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值