至少有K个重复字符的最长子串

给你一个字符串 s 和一个整数 k ,请你找出 s 中的最长子串, 要求该子串中的每一字符出现次数都不少于 k 。返回这一子串的长度。

如果不存在这样的子字符串,则返回 0。

示例 1:

复制代码
输入:s = "aaabb", k = 3
输出:3
解释:最长子串为 "aaa" ,其中 'a' 重复了 3 次。

示例 2:

复制代码
输入:s = "ababbc", k = 2
输出:5
解释:最长子串为 "ababb" ,其中 'a' 重复了 2 次, 'b' 重复了 3 次。

思路:滑动窗口,没有条件滑就创造条件滑。规定 t 为滑动窗口内最大的字母种类(当取所有可能的 t 时,也就相当于全集了,等于没规定这个条件)。虽然最大种类数为 t 时,t-1可能也会被考虑一些次,但是一定会考虑到长度 仅为 t 的所有情况。而最后的res 一定是某个数目的 字母种类 ,所以一定可以考虑到。

设置 existNum、reachNum 两个变量,一个是存在字母种类数量,一个数到达k数目要求的字母种类数

python 复制代码
from collections import defaultdict


class Solution:
    def longestSubstring(self, s: str, k: int) -> int:
        charSet = set(s)
        res = 0
        # 滑动窗口内最大的字母种类
        for t in range(1,len(charSet)+1):
            exist=defaultdict(int)
            existNum=0
            reachNum=0
            l,r=0,0
            while r<len(s):
                exist[s[r]] += 1
                # 存在
                if exist[s[r]] == 1:
                    existNum += 1
                # 满足
                if exist[s[r]] == k:
                    reachNum += 1

                while l<=r and existNum>t:
                    exist[s[l]]-=1
                    if exist[s[l]]==0:
                        existNum-=1

                    if exist[s[l]] == k-1:
                        reachNum -= 1
                    l += 1

                if reachNum==existNum:
                    res=max(res,r-l+1)
                r += 1

自己写的一版错误的:

没有最外层的 t ,这样滑 处理不了 "baaabcb" 这种情况。

python 复制代码
from collections import defaultdict


class Solution:
    def longestSubstring(self, s: str, k: int) -> int:
        res = 0
        characterDict=defaultdict(int)
        for c in s:
            characterDict[c]+=1
        exist=defaultdict(int)
        existNum=0
        reachNum=0
        l,r=0,0
        while r<=len(s):
            if r==len(s) or exist[s[r]]+characterDict[s[r]]<k:
                while l<r and reachNum<existNum:
                    exist[s[l]]-=1
                    if exist[s[l]]==0:
                        existNum-=1

                    if exist[s[l]] == k-1:
                        reachNum -= 1
                    l += 1

                res=max(res,r-l)
                l=r=r+1

            else:
                exist[s[r]] += 1
                #存在
                if exist[s[r]]==1:
                    existNum+=1

                #满足
                if exist[s[r]]==k:
                    reachNum+=1
                r+=1
        return res
print(Solution().longestSubstring("baaabcb", 3))
相关推荐
海梨花13 小时前
快手面试高频算法题
java·算法·面试
lqqjuly13 小时前
超分辨率算法深度解析(Super-Resolution Algorithms)
算法
加号313 小时前
【C#】 Web API 自定义配置函数请求路径:从路由本质到灵活架构设计
开发语言·c#
KANGBboy13 小时前
java知识四(面向对象编程)
android·java·开发语言
雪的季节13 小时前
矢量数据提取分析(甲方平台)
开发语言
一切皆是因缘际会13 小时前
AI智能新时代
数据结构·人工智能·ai·架构
ZC跨境爬虫13 小时前
跟着 MDN 学 JavaScript day_1:什么是 JavaScript?
开发语言·前端·javascript·ecmascript
qq_25183645713 小时前
基于java Web 日化商超库存管理系统设计与实现
java·开发语言·前端
Omics Pro14 小时前
3种蛋白结构输入方式!已申报欧洲发明专利
数据库·人工智能·python·机器学习·plotly
破土士V14 小时前
【Java基础语法10】继承、多态、抽象类接口、字符串与异常等
java·开发语言