3800. Minimum Cost to Make Two Binary Strings Equal Medium
1/**
2 * [3800] Minimum Cost to Make Two Binary Strings Equal
3 *
4 * You are given two binary strings s and t, both of length n, and three positive integers flipCost, swapCost, and crossCost.
5 * You are allowed to apply the following operations any number of times (in any order) to the strings s and t:
6 *
7 * 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.
8 * 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.
9 * Choose an index i and swap s[i] with t[i]. The cost of this operation is crossCost.
10 *
11 * Return an integer denoting the minimum total cost needed to make the strings s and t equal.
12 *
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">s = "01000", t = "10111", flipCost = 10, swapCost = 2, crossCost = 2</span>
16 * Output: <span class="example-io">16</span>
17 * Explanation:
18 * We can perform the following operations:
19 *
20 * Swap s[0] and s[1] (swapCost = 2). After this operation, s = "10000" and t = "10111".
21 * Cross swap s[2] and t[2] (crossCost = 2). After this operation, s = "10100" and t = "10011".
22 * Swap s[2] and s[3] (swapCost = 2). After this operation, s = "10010" and t = "10011".
23 * Flip s[4] (flipCost = 10). After this operation, s = t = "10011".
24 *
25 * The total cost is 2 + 2 + 2 + 10 = 16.
26 * </div>
27 * <strong class="example">Example 2:
28 * <div class="example-block">
29 * Input: <span class="example-io">s = "001", t = "110", flipCost = 2, swapCost = 100, crossCost = 100</span>
30 * Output: <span class="example-io">6</span>
31 * Explanation:
32 * Flipping all the bits of s makes the strings equal, and the total cost is 3 * flipCost = 3 * 2 = 6.
33 * </div>
34 * <strong class="example">Example 3:
35 * <div class="example-block">
36 * Input: <span class="example-io">s = "1010", t = "1010", flipCost = 5, swapCost = 5, crossCost = 5</span>
37 * Output: <span class="example-io">0</span>
38 * Explanation:
39 * The strings are already equal, so no operations are required.
40 * </div>
41 *
42 * Constraints:
43 *
44 * n == s.length == t.length
45 * 1 <= n <= 10^5
46 * 1 <= flipCost, swapCost, crossCost <= 10^9
47 * s and t consist only of the characters '0' and '1'.
48 *
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/minimum-cost-to-make-two-binary-strings-equal/
53// discuss: https://leetcode.com/problems/minimum-cost-to-make-two-binary-strings-equal/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58 pub fn minimum_cost(s: String, t: String, flip_cost: i32, swap_cost: i32, cross_cost: i32) -> i64 {
59
60 }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn test_3800() {
71 }
72}
73Back
© 2026 bowen.ge All Rights Reserved.