Example:
Input:
s = “ababbc”, k = 2
Output:
5
The longest substring is “ababb”, as ‘a’ is repeated 2 times and ‘b’ is repeated 3 times.
code:
class Solution:
def longestSubstring(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
for i in set(s):
if s.count(i) < k: # 找出不满足k次的字母
return max(self.longestSubstring(m, k) for m in s.split(i)) # 将其作为分割点进行分治
return len(s)
本文介绍了一种寻找字符串中满足至少出现k次的最长子串的算法。通过递归分割不符合条件的字符,最终找到符合条件的最长子串。示例中s=“ababbc”,k=2时,最长子串为“ababb”。该算法适用于字符串处理及子串查找场景。

1万+

被折叠的 条评论
为什么被折叠?



