Minimum Swaps to Make Strings Equal LeetCode solution in Rust

1
impl Solution {
2
pub fn minimum_swap(s1: String, s2: String) -> i32 {
3
let mut x1 = 0;
4
let mut y1 = 0;
5
for (c1, c2) in s1.chars().zip(s2.chars()) {
6
if c1 == 'x' && c2 == 'y' {
7
x1 += 1;
8
} else if c1 == 'y' && c2 == 'x' {
9
y1 += 1;
10
}
11
}
12
let mut ans = 0;
13
if x1 % 2 == 1 && y1 % 2 == 1 {
14
ans += 2;
15
x1 -= 1;
16
y1 -= 1;
17
}
18
ans += x1 / 2 + y1 / 2;
19
if x1 % 2 == 1 || y1 % 2 == 1 {
20
return -1;
21
}
22
ans
23
}
24
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0