Suppose we have a string s where each characters are sorted and we also have a number k, we have to find the length of the longest substring such that every character occurs at least k times.
So, if the input is like s = "aabccddeeffghij" k = 2, then the output will be 8, as the longest substring here is "ccddeeff" here every character occurs at least 2 times.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from collections import Counter class Solution: def solve(self, s, k): def rc(lst): c = Counter(lst) acc =  ans = 0 valid = True for x in lst: if c[x] < k: valid = False ans = max(ans, rc(acc)) acc =  else: acc.append(x) if valid: return len(acc) else: ans = max(ans, rc(acc)) return ans return rc(list(s)) ob = Solution() s = "aabccddeeffghij" k = 2 print(ob.solve(s, k))