FastPrepMost Frequent Valid Substring

Most Frequent Valid Substring

Visa logoVisa● MediumFULLTIMEOA
Learn

Problem statement

You are given a string s and integers minLength, maxLength, and maxUnique.

A substring is valid when:

  • its length is between minLength and maxLength, inclusive, and
  • it contains at most maxUnique distinct characters.

Return the greatest number of occurrences of any one valid substring in s. Occurrences may overlap. Return 0 if no substring is valid.

Function

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

Examples

Example 1

s = "aababcaab"minLength = 2maxLength = 3maxUnique = 2return = 3

The valid substring ab occurs three times, starting at indices 1, 3, and 7.

Example 2

s = "aaaa"minLength = 1maxLength = 3maxUnique = 1return = 4

The length-one substring a is valid and occurs four times.

Constraints

  • 1 <= s.length <= 104
  • 1 <= minLength <= maxLength <= s.length
  • 1 <= maxUnique <= 128
  • s contains printable ASCII characters.

More Visa problems

See Visa hiring insights
public int mostFrequentValidSubstring(String s, int minLength, int maxLength, int maxUnique) {
    // Write your code here.
}
s"aababcaab"
minLength2
maxLength3
maxUnique2
expected3
Checking account…