6
# ref: https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/discuss/457577/C%2B%2B-Greedy-approach-%2B-Sliding-window-O(n).9
# if a string have occurrences x times, any of its substring must also appear at least x times10
# there must be a substring of length minSize, that has the most occurrences11
# so that we just need to count the occurrences of all substring with length minSize15
# find the maximum occurrences of all substrings with length = minSize18
# use sliding window to lock down substrings to minSize limit (greedy approach)19
# then use a counter to count the occurences of valid substrings20
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:21
# dict to store valid substrings and their respective occurences count23
# find longest substrings that has k unique chars24
# counter dict for char count and make sure unique char count is not exceeded25
counter = defaultdict(int)26
# init left and right pointers of sliding window28
# iterate right pointer32
# invalid window found, so make it valid again33
# len of window is greater than minSize34
while (r - l + 1) > minSize:36
# remove the key from dict for unique char count if it becomes zero37
if counter[s[l]] == 0:39
# increment the left pointer to make it a valid window42
# valid window size (minSize) and unique char count is lesser than or equal to maxLetters43
if r - l + 1 == minSize and len(counter) <= maxLetters:44
# increment count of the occurence of the substring47
# make sure to update right pointer only after an iteration50
# return the count of substring with max occurence51
# edge case with no substring52
return max(d.values()) if d else 0