FastPrepFastPrep
Problem Brief

Get Max Occurrences

FULLTIMEOA
See IBM online assessment and hiring insights

Given a string containing a number of characters, find the substrings within the string that satisfy the conditions below:

  • The substring's length should be in the inclusive interval [minLength, maxLength].
  • The total number of unique characters should not exceed maxUnique.
  • Using those conditions, determine the frequency of the maximum occurring substring.

    Function Description

    Complete the function getMaxOccurrences in the editor below.

    getMaxOccurrences has the following parameter(s):

    • string components: the given string
    • int minLength: the minimum length of the substring
    • int maxLength: the maximum length of the substring
    • int maxUnique: the maximum unique characters of the substring

    Returns

    int: the maximum number of occurrences of any substring satisfying the conditions

    1Example 1

    Input
    components = "abcde", minLength = 2, maxLength = 4, maxUnique = 26
    Output
    1
    Explanation

    The given string is 'abcde'.

    The combination of components should be greater than or equal to 2, so 'a', 'b', 'c', 'd', and 'e' are discarded.

    The combination of components should be less than or equal to 4, so 'abcde' is discarded.

    The combinations of components that satisfy the conditions above are 'ab', 'bc', 'cd', 'de', 'abc', 'bcd', 'cde', 'abcd', 'bcde', and 'abcde'.

    Each combination of characters occurs only one time, so the number of occurrences is 1.

    2Example 2

    Input
    components = "ababab", minLength = 2, maxLength = 3, maxUnique = 4
    Output
    3
    Explanation

    The given string is 'ababab'.

    The combination of characters should be greater than or equal to 2, so a, b, a, b, a, and b are discarded.

    The combination of characters should be less than or equal to 3, so 'abab', 'baba', 'abab', 'baba', and 'ababa' are discarded.

    The combinations of components that satisfy the conditions above are 'ab', 'ba', 'ab', 'ba', 'ab', 'aba', 'bab', 'aba', 'bab', and 'bab'.

    The combination of characters 'ab' occurs 3 times, 'ba' and 'aba' and 'bab' 2 times. So, the maximum frequency of occurrence is 3.

    3Example 3

    Input
    components = "abcde", minLength = 2, maxLength = 3, maxUnique = 3
    Output
    1
    Explanation
    The given string components is 'abcde' The number of pieces in an interval should be greater than or equal to minLength = 2, so 'a', 'b', 'c', 'd', and 'e' The combination that satisy the conditions above are 'ab', 'bc', 'cd', 'de', 'abc', 'bcd', and 'cde' Each combination of characters occurs only one time, so the max number of occurances is 1.

    Constraints

    Limits and guarantees your solution can rely on.

  • 2 ≤ n ≤ 105
  • 2 ≤ minLengthmaxLength ≤ 26
  • maxLength < n
  • 2 ≤ maxUnique ≤ 26
  • public int getMaxOccurrences(String components, int minLength, int maxLength, int maxUnique) {
      // write your code here
    }
    
    Input

    components

    "abcde"

    minLength

    2

    maxLength

    4

    maxUnique

    26

    Output

    1

    Sign in to submit your solution.