Minimum Swaps to Make Strings Equal LeetCode solution in Java

1
class Solution {
2
public int minimumSwap(String s1, String s2) {
3

4
int count = 0;
5
int cnt_1 = 0;
6
int cnt_2 = 0;
7
char[] a = s1.toCharArray();
8
char[] b = s2.toCharArray();
9

10
for (int i = 0; i < a.length; i++) {
11
if (a[i] != b[i]) {
12
count++;
13
if (a[i] == 'x') cnt_1++;
14
else cnt_2++;
15
}
16
}
17

18
if (count % 2 == 1) return -1;
19
else return cnt_1 / 2 + cnt_2 / 2 + (cnt_1 % 2) * 2;
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0