FastPrepMaximum Valid Substring Frequency

Maximum Valid Substring Frequency

Microsoft logoMicrosoft● MediumFULLTIMEOA
Learn

Problem statement

Given a lowercase string text and three integers maxDistinct, minLength, and maxLength, return the maximum number of occurrences of any substring that satisfies both rules:

  • Its length is between minLength and maxLength, inclusive.
  • It contains at most maxDistinct distinct letters.

Occurrences may overlap. Return 0 when no substring satisfies the rules.

Function

maxSubstringFrequency(text: String, maxDistinct: int, minLength: int, maxLength: int) → int

Examples

Example 1

text = "aababcaab"maxDistinct = 2minLength = 3maxLength = 4return = 2

The substring aab appears twice, and it contains only the letters a and b.

Example 2

text = "aaaa"maxDistinct = 1minLength = 3maxLength = 3return = 2

The two length-three windows are both aaa, so overlapping occurrences produce frequency 2.

Example 3

text = "abcde"maxDistinct = 2minLength = 3maxLength = 3return = 0

Every length-three substring contains three distinct letters, so none is valid.

Constraints

  • 1 <= text.length() <= 10^5.
  • text contains only lowercase English letters.
  • 1 <= maxDistinct <= 26.
  • 1 <= minLength <= maxLength <= min(26, text.length()).

More Microsoft problems

See Microsoft hiring insights
public int maxSubstringFrequency(String text, int maxDistinct, int minLength, int maxLength) {
    // Write your code here.
}
text"aababcaab"
maxDistinct2
minLength3
maxLength4
expected2
Checking account…