Minimize Binary Subsequence Cost
Updated
Problem statement
You are given a string binaryString made only of '0', '1', and '!', and two integers x and y.
Replace every '!' with either '0' or '1'. Then add up the cost. Every subsequence equal to "01" costs x, and every subsequence equal to "10" costs y. Such a subsequence is any pair of positions, read left to right, that hold those two characters. The two positions do not have to be next to each other.
Return the smallest total cost you can get, 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 every '!' with the same character, for example all zeros. Then the string has no "01" or "10" subsequences, so the total cost is zero.
Constraints
1 <= binaryString.length <= 10^50 <= x, y <= 10^5binaryStringcontains only'0','1', and'!'.
More Amazon Online Assessment questions
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Get Smallest Base SegmentOA · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026