2896. Apply Operations to Make Two Strings Equal Medium
1/**
2 * [2896] Apply Operations to Make Two Strings Equal
3 *
4 * You are given two 0-indexed binary strings s1 and s2, both of length n, and a positive integer x.
5 * You can perform any of the following operations on the string s1 any number of times:
6 *
7 * Choose two indices i and j, and flip both s1[i] and s1[j]. The cost of this operation is x.
8 * Choose an index i such that i < n - 1 and flip both s1[i] and s1[i + 1]. The cost of this operation is 1.
9 *
10 * Return the minimum cost needed to make the strings s1 and s2 equal, or return -1 if it is impossible.
11 * Note that flipping a character means changing it from 0 to 1 or vice-versa.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: s1 = "1100011000", s2 = "0101001010", x = 2
16 * Output: 4
17 * Explanation: We can do the following operations:
18 * - Choose i = 3 and apply the second operation. The resulting string is s1 = "110<u>11</u>11000".
19 * - Choose i = 4 and apply the second operation. The resulting string is s1 = "1101<u>00</u>1000".
20 * - Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "<u>0</u>1010010<u>1</u>0" = s2.
21 * The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
22 *
23 * <strong class="example">Example 2:
24 *
25 * Input: s1 = "10110", s2 = "00011", x = 4
26 * Output: -1
27 * Explanation: It is not possible to make the two strings equal.
28 *
29 *
30 * Constraints:
31 *
32 * n == s1.length == s2.length
33 * 1 <= n, x <= 500
34 * s1 and s2 consist only of the characters '0' and '1'.
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/apply-operations-to-make-two-strings-equal/
40// discuss: https://leetcode.com/problems/apply-operations-to-make-two-strings-equal/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn min_operations(s1: String, s2: String, x: i32) -> i32 {
46 0
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_2896() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.