Making Two Binary Strings Equal

You are given two binary strings s and t of the same length, alongside three integers representing operation costs. The flipCost represents the price to flip any single bit in either string. The swapCost denotes the price to swap any two bits within the same string. Finally, the crossCost represents the price to swap a bit in s with the bit in t at the exact same index.

Return the minimum cost required to make both binary strings equal. You may apply any of the above operations any number of times.

Example 1:

Input: s = "01000", t = "10111", flipCost = 10, swapCost = 2, crossCost = 2

Output: 16

Explanation: We can make both strings exactly equal to "10011" through the following optimal sequence of operations: Swap the bits at index 0 and 1 in s (Cost: 2). s becomes "10000". Now Cross-swap the bits between s and t at index 2 (Cost: 2). s becomes "10100" and t becomes "10011". Swap the bits at index 2 and 3 in s (Cost: 2). s becomes "10010". Flip the bit at index 4 in s (Cost: 10). s becomes "10011".

Both strings are now "10011", and the accumulated minimum cost is 2 + 2 + 2 + 10 = 16.

Example 2:

Input: s = "001", t = "110", flipCost = 2, swapCost = 100, crossCost = 100

Output: 6

Explanation: Because swapCost and crossCost are absurdly expensive (100), the cheapest mathematical route is to ignore them entirely and simply flip all 3 mismatches individually. 3 * 2 = 6.

Now Your Turn!

Pick the correct output for the given input

Input: s = "00", t = "01", flipCost = 100, swapCost = 1, crossCost = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == s.length == t.length
  • 1 <= n <= 105​​​​​​​
  • 1 <= flipCost, swapCost, crossCost <= 109
  • s and t consist only of the characters '0' and '1'.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long minimumCost(string s, string t, int flipCost, int swapCost, int crossCost) {
// Write your code here
}
};
Test Case

Input:

Cross Cost
Flip Cost
S
Swap Cost
T