395. Longest Substring with At Least K Repeating Characters
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
solution
滑动窗口
from collections import defaultdict
class Solution:
def longestSubstring(self, s: str, k: int) -> int:
if not s: return 0
n = len(s)
max_num = len(set(s))
# 当不同字符个数为num,最长字符为多少
def cal(num):
left = 0
# 目前不同字符的个数
cur = 0
# 大于等于k字符的个数
ge_k = 0
# 记录字符个数
c = defaultdict(int)
# 最长字符串
res = 0
for right in range(n):
c[s[right]] += 1
# 不同字符个数
if c[s[right]] == 1:
cur += 1
# 大于等于k个数
if c[s[right]] == k:
ge_k += 1
# 当字符串不同个数大于 num
while cur > num:
if c[s[left]] == 1:
cur -= 1
if c[s[left]] == k:
ge_k -= 1
c[s[left]] -= 1
left += 1
if ge_k == num:
res = max(res, right - left + 1)
return res
return max(cal(num) for num in range(1, max_num + 1))时间复杂度:O() 空间复杂度:O()
分治
前缀和
Last updated