Minimum Cost to Make Two Binary Strings Equal Problem
#3800
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3800.
You are given two binary strings s and t, both of length n, and three positive integers flipCost, swapCost, and crossCost.
You are allowed to apply the following operations any number of times (in any order) to the strings s and t:
- Choose any index
iand flips[i]ort[i](change'0'to'1'or'1'to'0'). The cost of this operation isflipCost. - Choose two distinct indices
iandj, and swap eithers[i]ands[j]ort[i]andt[j]. The cost of this operation isswapCost. - Choose an index
iand swaps[i]witht[i]. The cost of this operation iscrossCost.
Return an integer denoting the minimum total cost needed to make the strings s and t equal.
Example 1:
Input: s = "01000", t = "10111", flipCost = 10, swapCost = 2, crossCost = 2
Output: 16
Explanation:
We can perform the following operations:
- Swap s[0] and s[1] (swapCost = 2). After this operation, s = "10000" and t = "10111".
- Cross swap s[2] and t[2] (crossCost = 2). After this operation, s = "10100" and t = "10011".
- Swap s[2] and s[3] (swapCost = 2). After this operation, s = "10010" and t = "10011".
- Flip s[4] (flipCost = 10). After this operation, s = t = "10011".
The total cost is 2 + 2 + 2 + 10 = 16.
Example 2:
Input: s = "001", t = "110", flipCost = 2, swapCost = 100, crossCost = 100
Output: 6
Explanation:
Flipping all the bits of s makes the strings equal, and the total cost is 3 * flipCost = 3 * 2 = 6.
Example 3:
Input: s = "1010", t = "1010", flipCost = 5, swapCost = 5, crossCost = 5
Output: 0
Explanation:
The strings are already equal, so no operations are required.
Constraints:
n == s.length == t.length1 <= n <= 10^51 <= flipCost, swapCost, crossCost <= 10^9sandtconsist only of the characters'0'and'1'.
Solution
Approach: Count the Two Kinds of Mismatch
Positions where s and t already agree can be left alone. The mismatches come in two kinds: s[i] = 0, t[i] = 1 and s[i] = 1, t[i] = 0. Let d0 and d1 be their counts, with mx the larger and mn the smaller.
There are three ways to finish, and we take the cheapest:
- Flip every mismatch:
(d0 + d1) * flipCost. - Fix
mnpairs of opposite kinds with one swap each, then flip themx - mnleftovers:mn * swapCost + (mx - mn) * flipCost. - Use cross swaps to move mismatches from the larger group to the smaller one until the groups are as even as possible (
avg = (mx + mn) / 2each), then pair them up with swaps and flip the odd one if any:(avg - mn) * crossCost + avg * swapCost + (mx + mn - 2 * avg) * flipCost.
Complexity: Time O(n), space O(1), where n is the length of the strings.
class Solution:
def minimumCost(
self, s: str, t: str, flipCost: int, swapCost: int, crossCost: int
) -> int:
diff = [0] * 2
for c1, c2 in zip(s, t):
if c1 != c2:
diff[int(c1)] += 1
ans = (diff[0] + diff[1]) * flipCost
mx = max(diff)
mn = min(diff)
ans = min(ans, mn * swapCost + (mx - mn) * flipCost)
avg = (mx + mn) // 2
ans = min(
ans,
(avg - mn) * crossCost + avg * swapCost + (mx + mn - avg * 2) * flipCost,
)
return ans