★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(let_us_code)
➤博主域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11831454.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You are given two strings s1 and s2 of 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: swap s1[i] and s2[j].
Return the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so.
Example 1:
Input: s1 = "xx", s2 = "yy"
Output: 1
Explanation:
Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".
Example 2:
Input: s1 = "xy", s2 = "yx"
Output: 2
Explanation:
Swap s1[0] and s2[0], s1 = "yy", s2 = "xx".
Swap s1[0] and s2[1], s1 = "xy", s2 = "xy".
Note that you can't swap s1[0] and s1[1] to make s1 equal to "yx", cause we can only swap chars in different strings.
Example 3:
Input: s1 = "xx", s2 = "xy"
Output: -1
Example 4:
Input: s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
Output: 4
Constraints:
1 <= s1.length, s2.length <= 1000
s1, s2 only contain 'x' or 'y'.
有两个长度相同的字符串 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'。
0ms
1 class Solution { 2 func minimumSwap(_ s1: String, _ s2: String) -> Int { 3 let c1 = Array(s1) 4 let c2 = Array(s2) 5 var xc = 0 6 var yc = 0 7 var xc1 = 0 8 var yc1 = 0 9 10 for i in 0 ..< c1.count { 11 if c1[i] != c2[i] { 12 if c1[i] == Character("x") { 13 xc += 1 14 xc1 += 1 15 } else { 16 yc += 1 17 yc1 += 1 18 } 19 if c2[i] == Character("x") { 20 xc += 1 21 } else { 22 yc += 1 23 } 24 } else { 25 if c1[i] == Character("x") { 26 xc += 2 27 } else { 28 yc += 2 29 } 30 } 31 } 32 33 if xc % 2 != 0 || yc % 2 != 0 { 34 return -1 35 } else { 36 return xc1 / 2 + yc1 / 2 + xc1 % 2 + yc1 % 2 37 } 38 } 39 }
1 class Solution { 2 func minimumSwap(_ s1: String, _ s2: String) -> Int { 3 var x1:Int = 0 // number of 'x' in s1 (skip equal chars at same index) 4 var y1:Int = 0 // number of 'y' in s1 (skip equal chars at same index) 5 var x2:Int = 0 // number of 'x' in s2 (skip equal chars at same index) 6 var y2:Int = 0 // number of 'y' in s2 (skip equal chars at same index) 7 let s1:[Character] = Array(s1) 8 let s2:[Character] = Array(s2) 9 for i in 0..<s1.count 10 { 11 let c1 = s1[i] 12 let c2 = s2[i] 13 if c1 == c2 {continue} 14 if c1 == "x" {x1 += 1} 15 else {y1 += 1} 16 if c2 == "x" {x2 += 1} 17 else {y2 += 1} 18 } 19 // After skip "c1 == c2", check the number of 'x' and 'y' left in s1 and s2. 20 if (x1 + x2) % 2 != 0 || (y1 + y2) % 2 != 0 21 { 22 return -1; // if number of 'x' or 'y' is odd, we can not make s1 equals to s2 23 } 24 // Cases to do 1 swap: 25 // "xx" => x1 / 2 => how many pairs of 'x' we have ? 26 // "yy" => y1 / 2 => how many pairs of 'y' we have ? 27 // 28 // Cases to do 2 swaps: 29 // "xy" or "yx" => x1 % 2 30 return x1 / 2 + y1 / 2 + (x1 % 2) * 2 31 } 32 }
4ms
1 class Solution { 2 func minimumSwap(_ s1: String, _ s2: String) -> Int { 3 let c1 = Array(s1) 4 let c2 = Array(s2) 5 var xc = 0 6 var yc = 0 7 8 for i in 0 ..< c1.count where c1[i] != c2[i] { 9 if c1[i] == Character("x") { 10 xc += 1 11 } else { 12 yc += 1 13 } 14 } 15 16 if (xc + yc) % 2 != 0 { 17 return -1 18 } else { 19 return xc / 2 + yc / 2 + 2 * (xc % 2) 20 } 21 } 22 }