Class Solution
-
- All Implemented Interfaces:
public final class Solution1247 - Minimum Swaps to Make Strings Equal\.
Medium
You are given two strings
s1ands2of equal length consisting of letters"x"and"y"only. Your task is to make these two strings equal to each other. You can swap any two characters that belong to different strings, which means: swaps1[i]ands2[j].Return the minimum number of swaps required to make
s1ands2equal, or return-1if it is impossible to do so.Example 1:
Input: s1 = "xx", s2 = "yy"
Output: 1
Explanation: Swap s10 and s21, s1 = "yx", s2 = "yx".
Example 2:
Input: s1 = "xy", s2 = "yx"
Output: 2
Explanation:
Swap s10 and s20, s1 = "yy", s2 = "xx".
Swap s10 and s21, s1 = "xy", s2 = "xy".
Note that you cannot swap s10 and s11 to make s1 equal to "yx", cause we can only swap chars in different strings.
Example 3:
Input: s1 = "xx", s2 = "xy"
Output: -1
Constraints:
1 <= s1.length, s2.length <= 1000s1, s2only contain'x'or'y'.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminimumSwap(String s1, String s2)-
-
Method Detail
-
minimumSwap
final Integer minimumSwap(String s1, String s2)
-
-
-
-