FastPrepMaximum Valid Substring Frequency

Maximum Valid Substring Frequency

Roblox logoRoblox● MediumFULLTIMEOA
Learn

Problem statement

A valid substring of s is contiguous, has length from minLength through maxLength inclusive, and contains at most maxUnique distinct letters.

For this exercise, assume occurrences at different starting positions are counted separately, including overlapping occurrences. Return the largest occurrence count of any one valid substring value. Return 0 if no substring is valid. The result is a count, so no tie rule between equally frequent substring values is needed.

Function

maxSubstringFrequency(s: String, minLength: int, maxLength: int, maxUnique: int) → int

Examples

Example 1

s = "aaaaa"minLength = 2maxLength = 3maxUnique = 1return = 4

aa occurs at four starting positions, including overlaps. aaa occurs three times, so the answer is 4.

Example 2

s = "aabcabcaab"minLength = 3maxLength = 5maxUnique = 2return = 2

aab occurs twice and uses only two distinct letters. It is a valid substring with the greatest frequency.

Example 3

s = "abc"minLength = 2maxLength = 2maxUnique = 1return = 0

Both length-2 substrings contain two distinct letters and exceed the limit 1.

Constraints

  • 1 <= s.length <= 10000; s contains lowercase English letters.
  • 1 <= minLength <= maxLength <= min(26, s.length).
  • 1 <= maxUnique <= 26.

More Roblox problems

See Roblox hiring insights
public int maxSubstringFrequency(String s, int minLength, int maxLength, int maxUnique) {
    // write your code here
}
s"aaaaa"
minLength2
maxLength3
maxUnique1
expected4
Checking account…