Problem
Minimize Binary Subsequence Cost
Learn this problemProblem statement
You are given a string binaryString consisting only of '0', '1', and '!', and two integers x and y.
Replace every '!' with either '0' or '1'. After replacement, every subsequence equal to "01" contributes cost x, and every subsequence equal to "10" contributes cost y.
Return the minimum possible total cost modulo 1_000_000_007.
Function
minimizeBinarySubsequenceCost(binaryString: String, x: int, y: int) → intExamples
Example 1
binaryString = "101!1"x = 2y = 3return = 9Replacing '!' with '0' gives cost 15. Replacing it with '1' gives cost 9, which is optimal.
Example 2
binaryString = "!!!!!"x = 2y = 3return = 0Replace all characters with the same bit, so there are no "01" or "10" subsequences.
Constraints
1 <= binaryString.length <= 10^50 <= x, y <= 10^5binaryStringcontains only'0','1', and'!'.
More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026