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

4 篇文章 0 订阅

思路,分治法。把字符串按照不符合条件的字符进行分割,最后剩下的最长的那段就是答案
思路其实比较简单,但是代码比较难理解。尤其是递归寻找的时候何时返回答案。
这里采用字典记录这段字串里的元素个数,如果字典里所有元素的个数都大于k说明符合条件

def longestSubstring(s, k):
    def dfs(s, l, r, k):
        if r-l+1<k:
            return 0
        counter = defaultdict(int)
        for i in range(l, r + 1):
            counter[s[i]] += 1
        if min(counter.values()) >= k:        #这里符合条件返回
            return r - l + 1
        maxLength = 0
        for i in range(l, r + 1):
            if counter[s[i]] < k:
                maxLength = max(maxLength, dfs(s, l, i - 1, k))
                l = i + 1
        if counter[s[r]] >= k:   #注意这里判断一下,防止后半段是符合条件的,如果没有会错过
            maxLength = max(maxLength, dfs(s, l, r, k))
        return maxLength
    n = len(s)
    return dfs(s, 0, n - 1, k)

简写

def longestSubstring(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        if not s:
            return 0
        for c in set(s):
            if s.count(c) < k:
                return max(self.longestSubstring(t ,k) for t in s.split(c))
        return len(s)

不使用系统栈

def longestSubstring(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        stack = [s]
        res = 0
        while stack:
            count = defaultdict(int)
            s = stack.pop()
            for i in s:
                count[i] += 1
            for j, c in count.items():
                if c < k:
                    stack.extend([t for t in s.split(j)])
                    break
            else:
                res = max(res, len(s))
        return res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值