|
1 |
| -2896\. |
| 1 | +2896\. Apply Operations to Make Two Strings Equal |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given two**0-indexed** binary strings`s1` and`s2`, both of length`n`, and a positive integer`x`. |
| 6 | + |
| 7 | +You can perform any of the following operations on the string`s1`**any** number of times: |
| 8 | + |
| 9 | +* Choose two indices`i` and`j`, and flip both`s1[i]` and`s1[j]`. The cost of this operation is`x`. |
| 10 | +* 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`. |
| 11 | + |
| 12 | +Return_the**minimum** cost needed to make the strings_`s1`_and_`s2`_equal, or return_`-1`_if it is impossible._ |
| 13 | + |
| 14 | +**Note** that flipping a character means changing it from`0` to`1` or vice-versa. |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +**Input:** s1 = "1100011000", s2 = "0101001010", x = 2 |
| 19 | + |
| 20 | +**Output:** 4 |
| 21 | + |
| 22 | +**Explanation:** We can do the following operations: |
| 23 | + |
| 24 | +- Choose i = 3 and apply the second operation. The resulting string is s1 = "110<ins>**11**</ins>11000". |
| 25 | + |
| 26 | +- Choose i = 4 and apply the second operation. The resulting string is s1 = "1101**<ins>00</ins>**1000". |
| 27 | + |
| 28 | +- Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "<ins>**0**</ins>1010010<ins>**1**</ins>0" = s2. |
| 29 | + |
| 30 | +The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible. |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +**Input:** s1 = "10110", s2 = "00011", x = 4 |
| 35 | + |
| 36 | +**Output:** -1 |
| 37 | + |
| 38 | +**Explanation:** It is not possible to make the two strings equal. |
| 39 | + |
| 40 | +**Constraints:** |
| 41 | + |
| 42 | +*`n == s1.length == s2.length` |
| 43 | +*`1 <= n, x <= 500` |
| 44 | +*`s1` and`s2` consist only of the characters`'0'` and`'1'`. |