leet-code/src/main/java/leetcode/editor/cn/MinimumSwapsToMakeStringsEqual.java

83 lines
2.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//有两个长度相同的字符串 s1 和 s2且它们其中 只含有 字符 "x" 和 "y",你需要通过「交换字符」的方式使这两个字符串相同。
//
// 每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。
//
// 交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换 s1[i] 和 s2[j],但不能交换 s1[i] 和 s1[
//j]。
//
// 最后,请你返回使 s1 和 s2 相同的最小交换次数,如果没有方法能够使得这两个字符串相同,则返回 -1 。
//
//
//
// 示例 1
//
// 输入s1 = "xx", s2 = "yy"
//输出1
//解释:
//交换 s1[0] 和 s2[1],得到 s1 = "yx"s2 = "yx"。
//
// 示例 2
//
// 输入s1 = "xy", s2 = "yx"
//输出2
//解释:
//交换 s1[0] 和 s2[0],得到 s1 = "yy"s2 = "xx" 。
//交换 s1[0] 和 s2[1],得到 s1 = "xy"s2 = "xy" 。
//注意,你不能交换 s1[0] 和 s1[1] 使得 s1 变成 "yx",因为我们只能交换属于两个不同字符串的字符。
//
// 示例 3
//
// 输入s1 = "xx", s2 = "xy"
//输出:-1
//
//
// 示例 4
//
// 输入s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
//输出4
//
//
//
//
// 提示:
//
//
// 1 <= s1.length, s2.length <= 1000
// s1, s2 只包含 'x' 或 'y'。
//
// Related Topics 贪心 数学 字符串 👍 45 👎 0
package leetcode.editor.cn;
//1247:交换字符使得字符串相同
class MinimumSwapsToMakeStringsEqual {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumSwapsToMakeStringsEqual().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minimumSwap(String s1, String s2) {
int x = 0;
int y = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
if (s1.charAt(i) == 'x') {
x++;
} else {
y++;
}
}
}
if ((x + y) % 2 == 1) {
return -1;
}
int min = Math.min(x, y);
return 2 * (min % 2) + x / 2 + y / 2;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}