395. Longest Substring with At Least K Repeating Characters

题目:

Given a string s and an integer k, return the length of the longest substring of s such that the frequency of each character in this substring is less than or equal to k.

 

Example 1:

Input: s = "aaabb", k = 3
Output: 3
Explanation: The longest substring is "aaa", as 'a' is repeated 3 times.

Example 2:

Input: s = "ababbc", k = 2
Output: 5
Explanation: The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.

 

Constraints:

  • 1 <= s.length <= 10^4
  • s consists of only lowercase English letters.
  • 1 <= k <= 10^5

 

思路:

题意大概是找到最长字串,子串中每个字母重复的次数不少于k。采用分而治之的方法。首先用hash map记录下每个字母出现的次数,然后去遍历字符串,这里要反着想,不是找符合条件的字母,而是去找不符合条件的字母,这样不符合条件的字母就成了我们分而治之的断点。在递归中,如果当前字符串都满足条件,则直接返回长度,否则找到断点后继续递归左边和右边。这里分段有两个点要注意,一是分段的那个点可以不被包括在左边和右边,因为它本身就不符合k;二是在找到第一个断点后,递归左边,这时候再往下遍历,找到第一个符合k的字母再分段给右边,如果不加这个第二点,最后一个case会超时。

 

代码:

class Solution {
public:
    int longestSubstring(string s, int k) {
        if(s.size()<k)
            return 0;
        unordered_map<char,int> record;
        for(auto i:s)
            record[i]++;
        bool found=true;
        int index;
        for(int i=0;i<s.size();i++)
        {
            if(record[s[i]]<k)
            {
                found=false;
                index=i;
                break;
            }
        }
        if(found)
            return s.size();
        int left=longestSubstring(s.substr(0,index),k);
        while(index<s.size()&&record[s[index]]<k)
            index++;
        int right=longestSubstring(s.substr(index),k);
        return max(left,right);
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值