395. Longest Substring with At Least K Repeating Characters class Solution: def longestSubstring(self, s: str, k: int) -> int: ans = 0 check = len(set(s)) for i in range(1,check+1): left = right = 0 window = collections.defaultdict(int) curr_ans = 0 k_value = 0 while right < len(s): window[s[right]] += 1 if window[s[right]] == k: k_value += 1 right += 1 while len(window) > i: if window[s[left]] == k: k_value -= 1 window[s[left]] -= 1 if window[s[left]] == 0: del window[s[left]] left += 1 if k_value == i: curr_ans = max(curr_ans,right - left) ans = max(curr_ans, ans) return ans