在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 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" Input: s1 = "xy", s2 = "yx" Input: s1 = "xx", s2 = "xy" Input: s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx" Constraints: 1 <= s1.length, s2.length <= 1000
有两个长度相同的字符串 s1 和 s2,且它们其中 只含有 字符 "x" 和 "y",你需要通过「交换字符」的方式使这两个字符串相同。 每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。 交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换 s1[i] 和 s2[j],但不能交换 s1[i] 和 s1[j]。 最后,请你返回使 s1 和 s2 相同的最小交换次数,如果没有方法能够使得这两个字符串相同,则返回 -1 。 示例 1: 输入:s1 = "xx", s2 = "yy" 输入:s1 = "xy", s2 = "yx" 输入:s1 = "xx", s2 = "xy" 输入:s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx" 提示: 1 <= s1.length, s2.length <= 1000 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 } Runtime: 4 ms
Memory Usage: 20.9 MB
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 }
|
请发表评论