Minimum Value Calculation by Replacing '!' in a Binary String
Updated
Problem statement
You are given a string s made of the characters '0', '1' and '!'. You must replace each '!' with either '0' or '1'. You are also given two integers x and y.
After the replacement, look at every pair of characters that sit next to each other:
- Each pair "01" adds
xto the total. - Each pair "10" adds
yto the total. - Pairs "00" and "11" add nothing.
Return the smallest total you can get.
For example, let s = "1!1", x = 2 and y = 5. Replace '!' with '1' to get "111". It has no "01" or "10" pair. The answer is 0.
Function
calculateMinimumValue(s: String, x: int, y: int) → int
Complete the function calculateMinimumValue in the editor.
calculateMinimumValue has the following parameters:
String s: the string of '0', '1' and '!' charactersint x: the amount added for each "01" pairint y: the amount added for each "10" pair
Returns
int: the smallest total after replacing every '!'
Examples
Example 1
s = "01!0"x = 2y = 3return = 5Replace '!' with '0' to get "0100". Its pairs are "01", "10" and "00", so the total is x + y = 2 + 3 = 5. Replace '!' with '1' to get "0110". Its pairs are "01", "11" and "10", so the total is also 2 + 3 = 5. The answer is 5.
Example 2
s = "!0!1"x = 3y = 4return = 3Replace both '!' with '0' to get "0001". Its pairs are "00", "00" and "01", so the total is x = 3. You cannot do better: the '0' at index 1 comes before the '1' at index 3, so some pair between them must be "01". The answer is 3.
Constraints
1 <= s.length <= 10^5- Every character of
sis'0','1'or'!' shas at least one'!'0 <= x <= 10^40 <= y <= 10^4
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Longest Subsequence That Is a SubstringBooking.com · OA
- Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
- Palindrome CounterGoldman Sachs · OAGoldman Sachs interview questions
- Longest String ChainVisa · OAVisa interview questions
- Minimum-Cost Digit String DecodingSquadStack.ai · OA
- Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
- Substrings Without a Target SubsequencePhonePe · OAPhonePe interview questions
- Minimum Edit-Distance String for Each QueryTarget · OA
- Longest Subsequence That Is a SubstringWells Fargo · OAWells Fargo interview questions
- Non-Alternating Binary PartitionsThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions