Given a string s of lowercase English letters, return the maximum number of occurrences of any substring that satisfies both of these rules:
-
The number of unique characters in the substring is less than or equal to
max_letters
.
-
The length of the substring is between
min_size
and
max_size
, inclusive.
Function Signature
def max_freq(s: str, max_letters: int, min_size: int, max_size: int) -> int:
Rules
-
Occurrences may overlap. Every starting index at which the substring appears counts as one occurrence.
-
If no substring of
s
satisfies both rules, return
0
.
Constraints
-
1 <= len(s) <= 10^5
-
1 <= max_letters <= 26
-
1 <= min_size <= max_size <= min(26, len(s))
-
s
consists only of lowercase English letters.
Examples
Input: s = "aababcaab", max_letters = 2, min_size = 3, max_size = 4
Output: 2
The substring "aab" starts at index 0 and at index 6. It has 2 unique letters and length 3, so it is eligible, and no eligible substring appears 3 times.
Input: s = "aaaa", max_letters = 1, min_size = 3, max_size = 3
Output: 2
"aaa" starts at index 0 and at index 1. The two occurrences overlap, and both count.
Input: s = "abcde", max_letters = 2, min_size = 3, max_size = 4
Output: 0
All five letters are different, so every substring of length 3 or 4 has at least 3 unique letters. No substring is eligible.