Maximum Number of Occurrences of a Substring - Complete Solution Guide
Maximum Number of Occurrences of a Substring is LeetCode problem 1297, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 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 max
Detailed Explanation
The problem asks us to find the maximum number of occurrences of a substring within a given string `s`, subject to two constraints: (1) the substring must contain no more than `maxLetters` unique characters, and (2) its length must be between `minSize` and `maxSize` (inclusive). The function needs to return the highest frequency of any substring meeting these criteria. The string `s` consists of lowercase English letters, and the substrings can overlap.
Solution Approach
The solution uses a sliding window approach to iterate through all possible substrings of length `minSize`. For each substring, it checks if the number of unique characters is less than or equal to `maxLetters`. If it is, the substring's occurrence count is incremented in a hash map. After iterating through all substrings, the solution finds the maximum value among the counts in the hash map, representing the maximum frequency of a valid substring. In essence, this approach finds all possible substring of size `minSize` and check them against the constraints, counting the ones that are valid, and returns the maximal count.
Step-by-Step Algorithm
- Step 1: Initialize a hash map (or Counter in Python) called `counts` to store the occurrence count of each valid substring.
- Step 2: Iterate through the input string `s` using a sliding window of size `minSize`. The loop variable `i` ranges from 0 to `n - minSize`, where `n` is the length of `s`.
- Step 3: For each window, extract the substring `substring = s[i:i + minSize]`.
- Step 4: Calculate the number of unique characters in `substring`. This can be done using a `set` in Python or Java, or an equivalent approach in C/C++.
- Step 5: Check if the number of unique characters is less than or equal to `maxLetters`. If this condition is met:
- Step 6: Increment the count of `substring` in the `counts` hash map. If the substring doesn't already exist as a key, add it with a count of 1. Otherwise, increment the existing count.
- Step 7: After iterating through all possible substrings, find the maximum value among the counts in the `counts` hash map.
- Step 8: Return the maximum count. If the hash map is empty (no valid substrings were found), return 0.
Key Insights
- Insight 1: Since we're looking for the *maximum* number of occurrences, we only need to consider substrings of length `minSize`. Substrings longer than `minSize` can be shortened to `minSize` without affecting the number of unique characters or the possibility of finding a matching substring, because any occurrence of a substring with length larger than minSize also implies occurrence of minSize substring within.
- Insight 2: Use a sliding window of size `minSize` to extract all possible substrings efficiently. Then, count occurrences of only the valid substrings which meet the unique character constraint.
- Insight 3: Using a hash map (or similar data structure like Python's `Counter`) is crucial for efficiently counting the occurrences of each valid substring.
Complexity Analysis
Time Complexity: O(n*minSize)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, String, Sliding Window.
Companies
Asked at: Atlassian, Hubspot, Roblox.