Count occurrences of the most frequent substring with limited distinct letters and bounded length

Quick Overview

Given a lowercase string, find how many times the most frequent substring occurs when substrings must stay within a limit on distinct letters and a range of lengths. It tests sliding window counting, hashing, and noticing which substring lengths actually need to be checked.

Count occurrences of the most frequent substring with limited distinct letters and bounded length

Company: AVEVA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s` of lowercase English letters, return the maximum number of occurrences of any substring that satisfies both of these rules: - The number of unique characters in the substring is less than or equal to `max_letters`. - The length of the substring is between `min_size` and `max_size`, inclusive. ### Function Signature ```python def max_freq(s: str, max_letters: int, min_size: int, max_size: int) -> int: ``` ### Rules - Occurrences may overlap. Every starting index at which the substring appears counts as one occurrence. - If no substring of `s` satisfies both rules, return `0`. ### Constraints - `1 <= len(s) <= 10^5` - `1 <= max_letters <= 26` - `1 <= min_size <= max_size <= min(26, len(s))` - `s` consists only of lowercase English letters. ### Examples ```text Input: s = "aababcaab", max_letters = 2, min_size = 3, max_size = 4 Output: 2 ``` The substring `"aab"` starts at index 0 and at index 6. It has 2 unique letters and length 3, so it is eligible, and no eligible substring appears 3 times. ```text Input: s = "aaaa", max_letters = 1, min_size = 3, max_size = 3 Output: 2 ``` `"aaa"` starts at index 0 and at index 1. The two occurrences overlap, and both count. ```text Input: s = "abcde", max_letters = 2, min_size = 3, max_size = 4 Output: 0 ``` All five letters are different, so every substring of length 3 or 4 has at least 3 unique letters. No substring is eligible.

Overview: Given a lowercase string, find how many times the most frequent substring occurs when substrings must stay within a limit on distinct letters and a range of lengths. It tests sliding window counting, hashing, and noticing which substring lengths actually need to be checked.

AVEVA logo
AVEVA
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s of lowercase English letters, return the maximum number of occurrences of any substring that satisfies both of these rules:

  • The number of unique characters in the substring is less than or equal to max_letters .
  • The length of the substring is between min_size and max_size , inclusive.

Function Signature

def max_freq(s: str, max_letters: int, min_size: int, max_size: int) -> int:

Rules

  • Occurrences may overlap. Every starting index at which the substring appears counts as one occurrence.
  • If no substring of s satisfies both rules, return 0 .

Constraints

  • 1 <= len(s) <= 10^5
  • 1 <= max_letters <= 26
  • 1 <= min_size <= max_size <= min(26, len(s))
  • s consists only of lowercase English letters.

Examples

Input: s = "aababcaab", max_letters = 2, min_size = 3, max_size = 4
Output: 2

The substring "aab" starts at index 0 and at index 6. It has 2 unique letters and length 3, so it is eligible, and no eligible substring appears 3 times.

Input: s = "aaaa", max_letters = 1, min_size = 3, max_size = 3
Output: 2

"aaa" starts at index 0 and at index 1. The two occurrences overlap, and both count.

Input: s = "abcde", max_letters = 2, min_size = 3, max_size = 4
Output: 0

All five letters are different, so every substring of length 3 or 4 has at least 3 unique letters. No substring is eligible.