#1297
Maximum Number of Occurrences of a Substring
specialist · 835 · lc medium +31 · verified · 54.3% accepted · 1,236 likes · top 47%
Description
Find the highest frequency of any substring of s satisfying these constraints:
- It uses at most maxLetters distinct characters.
- Its length falls in the range [minSize, maxSize].
Return the maximum occurrence count of any such substring.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 occurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Code
1
2
3