FastPrepGet Max Occurrences

Get Max Occurrences

IBM logoIBM● MediumFULLTIMEOA

Problem statement

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

getMaxOccurrences(components: String, minLength: int, maxLength: int, maxUnique: int) → int

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

Examples

Example 1

components = "abcde"minLength = 2maxLength = 4maxUnique = 26return = 1

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.

Example 2

components = "ababab"minLength = 2maxLength = 3maxUnique = 4return = 3

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.

Example 3

components = "abcde"minLength = 2maxLength = 3maxUnique = 3return = 1

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

  • 2 ≤ n ≤ 105
  • 2 ≤ minLength ≤ maxLength ≤ 26
  • maxLength < n
  • 2 ≤ maxUnique ≤ 26

More IBM problems

See IBM hiring insights
Saved
public int getMaxOccurrences(String components, int minLength, int maxLength, int maxUnique) {
  // write your code here
}
components"abcde"
minLength2
maxLength4
maxUnique26
expected1
Checking account…