Watch 8 video solutions for Maximum Number of Occurrences of a Substring, a medium level problem involving Hash Table, String, Sliding Window. This walkthrough by Coders Camp has 6,442 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a string s, return the maximum number of occurrences of any substring under the following rules:
maxLetters.minSize and maxSize inclusive.
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.
Constraints:
1 <= s.length <= 1051 <= maxLetters <= 261 <= minSize <= maxSize <= min(26, s.length)s consists of only lowercase English letters.Problem Overview: Given a string s, count how many times a substring appears while respecting two constraints: the substring length must be between minSize and maxSize, and it can contain at most maxLetters distinct characters. The goal is to return the maximum frequency of any valid substring.
Approach 1: Sliding Window Enumeration (O(n * (maxSize - minSize)), Space: O(n))
Use a sliding window to generate substrings between minSize and maxSize. For each window size, iterate across the string and track the number of unique characters using a frequency map. If the window satisfies the maxLetters constraint, record the substring in a hash table and increment its count. Continue scanning the string and keep track of the maximum frequency encountered.
This approach is straightforward because it directly follows the constraints in the problem statement. However, checking multiple window sizes increases the number of substrings processed. When maxSize - minSize is large, the total work grows quickly because each valid window requires substring extraction and hash map updates.
Approach 2: Hash Map Optimization with Fixed Window (O(n) time, O(n) space)
The key observation: the most frequent substring will always have length minSize. Longer substrings occur less frequently because they require more characters to match exactly. Once a substring of length minSize violates the maxLetters constraint, extending it only makes the condition harder to satisfy.
Use a fixed-size sliding window of length minSize. Maintain character counts inside the window and track the number of distinct characters. If the distinct count is ≤ maxLetters, extract the substring and update its count in a hash map. Slide the window by one character each step, updating the frequency map in constant time. Track the maximum occurrence as you go.
This reduces the search space dramatically. Instead of evaluating every possible substring length, you only evaluate windows of size minSize. Each character enters and leaves the window once, producing a linear scan over the string. Hash lookups remain constant time on average, making the entire algorithm O(n).
Recommended for interviews: The optimized sliding window with a hash map is the expected solution. It demonstrates pattern recognition and the ability to reduce unnecessary work using constraints. Explaining the naive enumeration approach first shows that you understand the problem space, but identifying that only minSize matters is the insight interviewers look for.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window Enumeration | O(n * (maxSize - minSize)) | O(n) | When exploring all valid substring lengths or demonstrating the brute-force logic |
| Hash Map + Fixed Sliding Window | O(n) | O(n) | Best general solution; uses the insight that only minSize substrings matter |