Maximum Valid Substring Frequency
Problem statement
Given a lowercase string text and three integers maxDistinct, minLength, and maxLength, return the maximum number of occurrences of any substring that satisfies both rules:
- Its length is between
minLengthandmaxLength, inclusive. - It contains at most
maxDistinctdistinct letters.
Occurrences may overlap. Return 0 when no substring satisfies the rules.
Function
maxSubstringFrequency(text: String, maxDistinct: int, minLength: int, maxLength: int) → intExamples
Example 1
text = "aababcaab"maxDistinct = 2minLength = 3maxLength = 4return = 2The substring aab appears twice, and it contains only the letters a and b.
Example 2
text = "aaaa"maxDistinct = 1minLength = 3maxLength = 3return = 2The two length-three windows are both aaa, so overlapping occurrences produce frequency 2.
Example 3
text = "abcde"maxDistinct = 2minLength = 3maxLength = 3return = 0Every length-three substring contains three distinct letters, so none is valid.
Constraints
1 <= text.length() <= 10^5.textcontains only lowercase English letters.1 <= maxDistinct <= 26.1 <= minLength <= maxLength <= min(26, text.length()).