Class Solution
-
- All Implemented Interfaces:
public final class Solution2896 - Apply Operations to Make Two Strings Equal\.
Medium
You are given two 0-indexed binary strings
s1ands2, both of lengthn, and a positive integerx.You can perform any of the following operations on the string
s1any number of times:Choose two indices
iandj, and flip boths1[i]ands1[j]. The cost of this operation isx.Choose an index
isuch thati < n - 1and flip boths1[i]ands1[i + 1]. The cost of this operation is1.
Return the minimum cost needed to make the strings
s1ands2equal, or return-1if it is impossible.Note that flipping a character means changing it from
0to1or vice-versa.Example 1:
Input: s1 = "1100011000", s2 = "0101001010", x = 2
Output: 4
Explanation: We can do the following operations:
Choose i = 3 and apply the second operation. The resulting string is s1 = "110<ins> 11 </ins>11000".
Choose i = 4 and apply the second operation. The resulting string is s1 = "1101**<ins>00</ins>**1000".
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.
The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
Example 2:
Input: s1 = "10110", s2 = "00011", x = 4
Output: -1
Explanation: It is not possible to make the two strings equal.
Constraints:
n == s1.length == s2.length1 <= n, x <= 500s1ands2consist only of the characters'0'and'1'.