Problem

Minimize Binary Subsequence Cost

Learn this problem
AmazonFULLTIMEOA
See Amazon hiring insights

Problem 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) → int

Examples

Example 1

binaryString = "101!1"x = 2y = 3return = 9

Replacing '!' with '0' gives cost 15. Replacing it with '1' gives cost 9, which is optimal.

Example 2

binaryString = "!!!!!"x = 2y = 3return = 0

Replace all characters with the same bit, so there are no "01" or "10" subsequences.

Constraints

  • 1 <= binaryString.length <= 10^5
  • 0 <= x, y <= 10^5
  • binaryString contains only '0', '1', and '!'.

More Amazon problems

drafts saved locally
public int minimizeBinarySubsequenceCost(String binaryString, int x, int y) {
  // write your code here
}
binaryString"101!1"
x2
y3
expected9
checking account