Most Frequent Valid Substring
Problem statement
You are given a string s and integers minLength, maxLength, and maxUnique.
A substring is valid when:
- its length is between
minLengthandmaxLength, inclusive, and - it contains at most
maxUniquedistinct characters.
Return the greatest number of occurrences of any one valid substring in s. Occurrences may overlap. Return 0 if no substring is valid.
Function
mostFrequentValidSubstring(s: String, minLength: int, maxLength: int, maxUnique: int) → intExamples
Example 1
s = "aababcaab"minLength = 2maxLength = 3maxUnique = 2return = 3The valid substring ab occurs three times, starting at indices 1, 3, and 7.
Example 2
s = "aaaa"minLength = 1maxLength = 3maxUnique = 1return = 4The length-one substring a is valid and occurs four times.
Constraints
1 <= s.length <= 1041 <= minLength <= maxLength <= s.length1 <= maxUnique <= 128scontains printable ASCII characters.