Quick Overview

Find how often the most frequent eligible substring appears in a lowercase string, where an eligible substring has a length within a given range and no more than a set number of distinct letters. Overlapping occurrences count and inputs reach 100,000 characters, so the task tests efficient substring counting.

Most Frequent Substring Under Distinct-Letter and Length Limits

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

Given a lowercase string `s`, consider only the substrings that satisfy two limits, and return how many times the most frequent of them occurs in `s`. A substring `t` of `s` is **eligible** when both of the following hold: - the number of distinct characters in `t` is at most `max_letters`; - the length of `t` is between `min_size` and `max_size`, inclusive. Return the largest number of occurrences of any eligible substring, or `0` if no substring of `s` is eligible. ### Function Signature ```python def max_substring_occurrences(s: str, max_letters: int, min_size: int, max_size: int) -> int: ``` ### Rules - Occurrences are counted by starting index. Two occurrences are different if they start at different indices, even when they overlap: in `"aaaa"`, the substring `"aaa"` starts at indices 0 and 1, so it occurs 2 times. - Only the count is returned, not the substring, so the answer is a single well-defined integer even when several eligible substrings share the maximum count. ### Constraints - `1 <= len(s) <= 100000` - `s` contains only lowercase English letters `a` through `z`. - `1 <= max_letters <= 26` - `1 <= min_size <= max_size <= min(26, len(s))` - The answer is an integer between `0` and `len(s)`, inclusive. ### Examples **Example 1** Input: `s = "aababcaab"`, `max_letters = 2`, `min_size = 3`, `max_size = 4` Output: `2` Explanation: `"aab"` has 2 distinct letters and length 3, and it starts at indices 0 and 6. Every other eligible substring occurs at most twice. **Example 2** Input: `s = "aaaa"`, `max_letters = 1`, `min_size = 3`, `max_size = 3` Output: `2` Explanation: `"aaa"` starts at indices 0 and 1; overlapping occurrences both count. **Example 3** Input: `s = "abcde"`, `max_letters = 2`, `min_size = 3`, `max_size = 3` Output: `0` Explanation: every length-3 substring (`"abc"`, `"bcd"`, `"cde"`) has 3 distinct letters, so no substring is eligible.

Overview: Find how often the most frequent eligible substring appears in a lowercase string, where an eligible substring has a length within a given range and no more than a set number of distinct letters. Overlapping occurrences count and inputs reach 100,000 characters, so the task tests efficient substring counting.

Given a string `s` of lowercase English letters and three integers `max_letters`, `min_size` and `max_size`, consider only the substrings of `s` that satisfy two limits, and return how many times the most frequent of them occurs in `s`. A substring `t` of `s` is **eligible** when both of the following hold: - the number of distinct characters in `t` is at most `max_letters`; - the length of `t` is between `min_size` and `max_size`, inclusive. Return the largest number of occurrences of any eligible substring, or `0` if no substring of `s` is eligible. Implement `max_substring_occurrences(s, max_letters, min_size, max_size)`, which returns an integer. ### Rules - Occurrences are counted by starting index. Two occurrences are different if they start at different indices, even when they overlap: in `"aaaa"`, the substring `"aaa"` starts at indices 0 and 1, so it occurs 2 times. - Only the count is returned, not the substring, so the answer is a single well-defined integer even when several eligible substrings share the maximum count. ### Examples **Example 1** Input: `s = "aababcaab"`, `max_letters = 2`, `min_size = 3`, `max_size = 4` Output: `2` Explanation: `"aab"` has 2 distinct letters and length 3, and it starts at indices 0 and 6. Every other eligible substring occurs at most twice. **Example 2** Input: `s = "aaaa"`, `max_letters = 1`, `min_size = 3`, `max_size = 3` Output: `2` Explanation: `"aaa"` starts at indices 0 and 1; overlapping occurrences both count. ### Constraints - `1 <= len(s) <= 100000` - `s` contains only lowercase English letters `a` through `z`. - `1 <= max_letters <= 26` - `1 <= min_size <= max_size <= min(26, len(s))` - The answer is an integer between `0` and `len(s)`, inclusive, so it never exceeds 2^31 - 1 and fits in a 32-bit `int` in every language.

Constraints

  • 1 <= len(s) <= 100000
  • s contains only lowercase English letters 'a' through 'z'.
  • 1 <= max_letters <= 26
  • 1 <= min_size <= max_size <= min(26, len(s))
  • The answer is an integer between 0 and len(s), inclusive; it never exceeds 2^31 - 1, so a 32-bit int suffices in every language.

Examples

Input: ('a', 1, 1, 1)

Expected Output: 1

Explanation: Minimum valid input: the one-letter substring 'a' is eligible and occurs once.

Input: ('abc', 3, 3, 3)

Expected Output: 1

Explanation: min_size = max_size = len(s): the whole string is the only candidate, and its 3 distinct letters equal max_letters, so it is eligible once.

Hints

  1. Occurrences are counted by starting index, so overlapping copies of the same substring each count separately.
  2. Both limits must hold at once: a substring of allowed length with too many distinct letters is not eligible, and neither is a substring with few letters but a length outside [min_size, max_size].
  3. Only the count is returned, so when several eligible substrings tie for the most occurrences, it does not matter which one you pick.

Loading coding console...

Show the approach

Approach

Key observation: suppose an eligible substring t of length L > min_size starts at some set of indices. Its prefix of length min_size starts at every one of those indices too, has no more distinct letters than t, and has length min_size, which lies in [min_size, max_size]; so that prefix is eligible and occurs at least as often as t. Therefore the answer equals the largest occurrence count among eligible substrings of length exactly min_size, and max_size never changes the answer.

Algorithm: slide a window of length min_size across s. Maintain a 26-entry letter-frequency array and the number of distinct letters in the window (increment it when a letter's count goes from 0 to 1, decrement it when a count drops from 1 to 0). Each time the window is full (its end index i is at least min_size - 1) and its distinct count is at most max_letters, increment that window's entry in a hash map keyed by the window text, and keep the largest value seen.

Correctness: every start index from 0 through len(s) - min_size is visited exactly once, so occurrences are counted by starting index and overlapping copies each count, exactly as the rules require. The map value for a window text is its number of eligible starting positions, and the observation above shows no longer eligible substring can beat the best length-min_size one. If no window qualifies, the map stays empty and the result is 0.

Edge cases: min_size = len(s) (a single window, answer 0 or 1); max_letters = 26 (the distinct-letter limit never binds); a single repeated letter (the answer can reach len(s) - min_size + 1, up to len(s) when min_size = 1); several substrings tied for the maximum (only the count is returned).

Time complexity:
O(n * min_size)
Space complexity:
O(n * min_size)