Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Move from brute-force thinking to an efficient approach using greedy strategy.
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 <= 1051 <= flipCost, swapCost, crossCost <= 109s and t consist only of the characters '0' and '1'.Problem summary: 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 i and flip s[i] or t[i] (change '0' to '1' or '1' to '0'). The cost of this operation is flipCost. Choose two distinct indices i and j, and swap either s[i] and s[j] or t[i] and t[j]. The cost of this operation is swapCost. Choose an index 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"01000" "10111" 10 2 2
"001" "110" 2 100 100
"1010" "1010" 5 5 5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3800: Minimum Cost to Make Two Binary Strings Equal
class Solution {
public long minimumCost(String s, String t, int flipCost, int swapCost, int crossCost) {
long[] diff = new long[2];
int n = s.length();
for (int i = 0; i < n; i++) {
char c1 = s.charAt(i), c2 = t.charAt(i);
if (c1 != c2) {
diff[c1 - '0']++;
}
}
long ans = (diff[0] + diff[1]) * flipCost;
long mx = Math.max(diff[0], diff[1]);
long mn = Math.min(diff[0], diff[1]);
ans = Math.min(ans, mn * swapCost + (mx - mn) * flipCost);
long avg = (mx + mn) / 2;
ans = Math.min(
ans, (avg - mn) * crossCost + avg * swapCost + (mx + mn - avg * 2) * flipCost);
return ans;
}
}
// Accepted solution for LeetCode #3800: Minimum Cost to Make Two Binary Strings Equal
func minimumCost(s string, t string, flipCost int, swapCost int, crossCost int) int64 {
var diff [2]int64
n := len(s)
for i := 0; i < n; i++ {
if s[i] != t[i] {
diff[s[i]-'0']++
}
}
ans := (diff[0] + diff[1]) * int64(flipCost)
mx := max(diff[0], diff[1])
mn := min(diff[0], diff[1])
ans = min(ans, mn*int64(swapCost)+(mx-mn)*int64(flipCost))
avg := (mx + mn) / 2
ans = min(ans, (avg-mn)*int64(crossCost)+avg*int64(swapCost)+(mx+mn-avg*2)*int64(flipCost))
return ans
}
# Accepted solution for LeetCode #3800: Minimum Cost to Make Two Binary Strings Equal
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
// Accepted solution for LeetCode #3800: Minimum Cost to Make Two Binary Strings Equal
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3800: Minimum Cost to Make Two Binary Strings Equal
// class Solution {
// public long minimumCost(String s, String t, int flipCost, int swapCost, int crossCost) {
// long[] diff = new long[2];
// int n = s.length();
// for (int i = 0; i < n; i++) {
// char c1 = s.charAt(i), c2 = t.charAt(i);
// if (c1 != c2) {
// diff[c1 - '0']++;
// }
// }
//
// long ans = (diff[0] + diff[1]) * flipCost;
//
// long mx = Math.max(diff[0], diff[1]);
// long mn = Math.min(diff[0], diff[1]);
// ans = Math.min(ans, mn * swapCost + (mx - mn) * flipCost);
//
// long avg = (mx + mn) / 2;
// ans = Math.min(
// ans, (avg - mn) * crossCost + avg * swapCost + (mx + mn - avg * 2) * flipCost);
// return ans;
// }
// }
// Accepted solution for LeetCode #3800: Minimum Cost to Make Two Binary Strings Equal
function minimumCost(
s: string,
t: string,
flipCost: number,
swapCost: number,
crossCost: number,
): number {
const diff: number[] = [0, 0];
const n = s.length;
for (let i = 0; i < n; i++) {
if (s[i] !== t[i]) {
diff[s.charCodeAt(i) - 48]++;
}
}
let ans = (diff[0] + diff[1]) * flipCost;
const mx = Math.max(diff[0], diff[1]);
const mn = Math.min(diff[0], diff[1]);
ans = Math.min(ans, mn * swapCost + (mx - mn) * flipCost);
const avg = (mx + mn) >> 1;
ans = Math.min(ans, (avg - mn) * crossCost + avg * swapCost + (mx + mn - avg * 2) * flipCost);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.