Watch 8 video solutions for Minimum Cost to Make Two Binary Strings Equal, a medium level problem involving String, Greedy. This walkthrough by Sanyam IIT Guwahati has 1,385 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
i and flip s[i] or t[i] (change '0' to '1' or '1' to '0'). The cost of this operation is flipCost.i and j, and swap either s[i] and s[j] or t[i] and t[j]. The cost of this operation is swapCost.i and swap s[i] with t[i]. The cost of this operation is crossCost.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:
s[0] and s[1] (swapCost = 2). After this operation, s = "10000" and t = "10111".s[2] and t[2] (crossCost = 2). After this operation, s = "10100" and t = "10011".s[2] and s[3] (swapCost = 2). After this operation, s = "10010" and t = "10011".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 <= 105​​​​​​​1 <= flipCost, swapCost, crossCost <= 109s and t consist only of the characters '0' and '1'.Problem Overview: You are given two binary strings of equal length. Some positions differ. The task is to perform operations with a defined cost to resolve these mismatches so both strings become identical while minimizing the total cost.
Approach 1: Brute Force Pairing of Mismatches (O(n^2) time, O(n) space)
Start by scanning both strings and recording every index i where s[i] != t[i]. These are the positions that must be fixed. A brute force strategy tries pairing mismatches in every possible way and computes the cost for each pairing combination. This works because resolving two mismatches together can sometimes be cheaper than fixing them independently. The drawback is the combinatorial explosion: evaluating many pairing possibilities leads to O(n^2) time or worse when the mismatch count grows.
Approach 2: Greedy Pairing of Adjacent Mismatches (O(n) time, O(n) space)
The key observation: only the indices where the strings differ matter. Store all mismatch indices in an array while iterating once through the strings. Instead of exploring every pairing, process mismatches in order and greedily pair nearby ones when doing so reduces the total cost. The greedy rule works because pairing closer mismatches minimizes the operation cost compared to resolving them independently. This reduces the decision at each step to a simple local comparison, resulting in a linear pass through the mismatch list.
This approach relies heavily on sequential processing of indices and careful cost comparison. You iterate through the mismatch positions, evaluate whether combining the current mismatch with the previous unresolved one is cheaper than fixing both separately, and accumulate the minimum cost.
The algorithm is fundamentally a greedy strategy operating on mismatch positions discovered through a single scan of the string. Because each index is processed at most once, the runtime stays linear and memory usage remains small.
Recommended for interviews: The greedy linear scan is the solution interviewers expect. Showing the brute force pairing demonstrates you understand that mismatches must be paired or fixed individually. Transitioning to the greedy observation—processing mismatches in order and minimizing cost locally—shows the optimization step and strong algorithmic reasoning.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Mismatch Pairing | O(n^2) | O(n) | Useful for understanding the pairing idea and validating smaller inputs |
| Greedy Pairing of Mismatches | O(n) | O(n) | Best general solution for large strings and interview settings |