Minimum Swaps to Make Strings Equal LeetCode solution in C++

1
class Solution {
2
public:
3
int minimumSwap(string s1, string s2) {
4
int n = s1.size();
5
int cnt1 = 0, cnt2 = 0, i = 0;
6

7
while (i < n) {
8
int x = s1[i];
9
int y = s2[i++];
10
if (x == 'x' and y == 'y') cnt1++;
11
if (x == 'y' and y == 'x') cnt2++;
12
}
13

14
if ((cnt1 + cnt2) % 2 == 1) return -1;
15

16
return (cnt1 / 2) + (cnt2 / 2) + (cnt1 % 2 + cnt2 % 2);
17
}
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0