Skip to content

Commit a688a62

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 41.80 MB (Top 5.91%)
1 parent 4beafa3 commit a688a62

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 41.80 MB (Top 5.91%)
2+
13
class Solution {
24
public int minimumSwap(String s1, String s2) {
3-
if(s1.length() != s2.length()) return -1;
4-
int n = s1.length();
5-
int x = 0 , y = 0;
65

7-
for(int i = 0 ; i < n ; i ++){
8-
char c1 = s1.charAt(i) , c2 = s2.charAt(i);
9-
if(c1 == 'x' && c2 == 'y') x++;
10-
else if(c1 == 'y' && c2 == 'x') y++;
6+
int count = 0;
7+
int cnt_1 = 0;
8+
int cnt_2 = 0;
9+
char[] a = s1.toCharArray();
10+
char[] b = s2.toCharArray();
11+
12+
for(int i=0;i<a.length;i++){
13+
if(a[i] != b[i]){
14+
count++;
15+
if(a[i] == 'x') cnt_1++;
16+
else cnt_2++;
17+
}
1118
}
1219

13-
if(x % 2 == 0 && y % 2 == 0) return x/2 + y/2;
14-
else if(x % 2 == 1 && y % 2 == 1) return x/2 + y/2 + 2;
15-
return -1;
20+
if(count%2 == 1)
21+
return -1;
22+
else return cnt_1/2+cnt_2/2+(cnt_1%2)*2;
1623
}
1724
}

0 commit comments

Comments
 (0)