Problem Brief
Minimum Value Calculation by Replacing '!' in a Binary String
FULLTIMEOA
You are given a string consisting of '0', '1', and '!', where '!' represents a character that can be replaced with either '0' or '1'. Additionally, you are given two integers x and y.
Your task is to replace all '!' characters in such a way that you minimize the calculated value based on the following rules:
x.y.Function Description
Complete the function calculateMinimumValue in the editor.
calculateMinimumValue has the following parameters:
- 1.
String s: a binary string containing '0', '1', and '!' characters - 2.
int x: the contribution value for '01' pairs - 3.
int y: the contribution value for '10' pairs
Returns
int: the minimum calculated value after replacing all '!' characters
1Example 1
Input
s = "01!0", x = 2, y = 3
Output
8
Explanation
Replace '!' with '0' to get '0100':
Pairs: ('01', '10') → Contribution: ( 2 imes 1 + 2 imes 3 = 8 )
Replace '!' with '1' to get '0110':
Pairs: ('01', '10') → Contribution: ( 2 imes 2 + 2 imes 3 = 10 )
The minimum contribution is ( 8 ).
2Example 2
Input
s = "!0!1", x = 3, y = 4
Output
7
Explanation
Replace the first '!' with '1' and the second '!' with '0' to get '10' and '01':
Pairs: ('10', '01') → Contribution: ( 3 + 4 = 7 )
Constraints
Limits and guarantees your solution can rely on.
The string contains at least one '!', and its length is between 1 and ( 10^5 )( x ) and ( y ) are non-negative integers