Given a string s and integers maxLetters, minSize, and maxSize, consider all substrings of s whose length is between minSize and maxSize inclusive. A substring is valid if it contains at most maxLetters distinct characters.
Return the maximum number of times any valid substring occurs in s.
Note: Occurrences of a substring may overlap.
Constraints:
1≤ s.length ≤105
1≤ maxLetters ≤26
1≤ minSize ≤ maxSize ≤min(26,ext‘s.length‘)
s consists of only lowercase English letters.
Input: s = "abcabcabc"-str, maxLetters = 3-int, minSize = 3-int, maxSize = 5-int
Output: 3-int
Explanation: Considering valid substrings of length 3 to 5 with at most 3 distinct letters, the substring "abc" (length 3) appears 3 times (positions 0–2, 3–5, 6–8), and no other valid substring occurs more frequently, so the answer is 3.
Input: s = "zzzzzz"-str, maxLetters = 1-int, minSize = 2-int, maxSize = 4-int
Output: 5-int
Explanation: Any substring made only of 'z' has 1 distinct letter, so it’s valid. The substring "zz" (length 2) occurs 5 times in "zzzzzz" (overlapping occurrences), which is the maximum.
Input: s = "abacabadabacaba"-str, maxLetters = 2-int, minSize = 3-int, maxSize = 3-int
Output: 4-int
Explanation: Only length-3 substrings with at most 2 distinct letters are considered. The substring "aba" is valid (letters {a,b}) and occurs 4 times in the string, which is the maximum frequency among valid length-3 substrings.
Input: s = "abcdefghijklmnopqrstuvwxyz"-str, maxLetters = 26-int, minSize = 1-int, maxSize = 26-int
Output: 1-int
Explanation: All substrings are valid since up to 26 distinct letters are allowed. However, because all characters are unique, no substring repeats, so the maximum occurrence count of any valid substring is 1.
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: