Get Max Occurrences
Learn this problemProblem statement
Given a string containing a number of characters, find the substrings within the string that satisfy the conditions below:
minLength, maxLength].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 = 1The 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 = 3The 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 = 1Constraints
2 ≤ n ≤ 1052 ≤ minLength ≤ maxLength ≤ 26maxLength < nmaxUnique ≤ 26More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026