Get Optimal String Length
A string is to be constructed using only the characters 'A' and 'B'. Given four integers,
countA, countB, maxA, maxB, the constructed
string is said to be optimal if:
countA 'A' characters, and countB 'B' characters.maxA 'A' characters.maxB 'B' characters.HackerRank organized fun trivia for its employees where it asked for the maximum possible length of an optimal string that can be constructed satisfying the criteria above. The goal is to find the maximum possible length of an optimal string.
Note:
- There can be multiple optimal strings with the same maximal length.
- A substring of a string is a contiguous subsegment of the string.
Complete the function getOptimalStringLength in the editor.
getOptimalStringLength has the following parameters:
int countA: the maximum count of character 'A'int countB: the maximum count of character 'B'int maxA: the maximum substring length of character 'A'int maxB: the maximum substring length of character 'B'
Returns
long integer: the maximum length of optimal string that can be constructed
1Example 1
2Example 2
Constraints
Limits and guarantees your solution can rely on.
0 ≤ countA, countB, maxA, maxB ≤ 106