Maximum Number of Occurrences of a Substring - Problem
Given a string s, return the maximum number of occurrences of any substring under the following rules:
- The number of unique characters in the substring must be less than or equal to
maxLetters. - The substring size must be between
minSizeandmaxSizeinclusive.
Input & Output
Example 1 — Basic Case
$
Input:
s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
›
Output:
2
💡 Note:
Substring "aab" appears twice and has 2 unique characters (≤ maxLetters), length 3 (within range). Other valid substrings appear only once.
Example 2 — No Valid Substrings
$
Input:
s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
›
Output:
2
💡 Note:
Substring "aaa" appears 2 times (positions 0-2 and 1-3), has 1 unique character (≤ maxLetters), length 3 (within range).
Example 3 — All Different Characters
$
Input:
s = "abcde", maxLetters = 2, minSize = 2, maxSize = 3
›
Output:
1
💡 Note:
Substrings of length 2: "ab", "bc", "cd", "de" each have exactly 2 unique characters (≤ maxLetters), and each appears once. Maximum count is 1.
Constraints
- 1 ≤ s.length ≤ 105
- 1 ≤ maxLetters ≤ 26
- 1 ≤ minSize ≤ maxSize ≤ min(26, s.length)
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code