Given a binary string s, return _theminimum number of character swaps to make it alternating , or _-1if it is impossible.
The string is called alternating if no two adjacent characters are equal.
For example, the strings "010" and "1010" are alternating, while the string "0100" is not.
Any two characters may be swapped, even if they are not adjacent.
To make the string alternating, we need to match the counts of ‘0’s and ‘1’s for possible patterns (starting with ‘0’ or ‘1’). For each pattern, count mismatches and compute swaps needed. If impossible, return -1.
#include<string>usingnamespace std;
classSolution {
public:int minSwaps(string s) {
int c0 =0, c1 =0;
for (char ch : s) (ch =='0'? c0 : c1)++;
if (abs(c0 - c1) >1) return-1;
int m1 =0, m2 =0;
for (int i =0; i < s.size(); ++i) {
if (s[i] != (i %2?'1':'0')) m1++;
if (s[i] != (i %2?'0':'1')) m2++;
}
int res =1e9;
if (c0 >= c1) res = min(res, m1 /2);
if (c1 >= c0) res = min(res, m2 /2);
return res ==1e9?-1: res;
}
};
classSolution {
publicintminSwaps(String s) {
int c0 = 0, c1 = 0;
for (char ch : s.toCharArray()) {
if (ch =='0') c0++;
else c1++;
}
if (Math.abs(c0 - c1) > 1) return-1;
int m1 = 0, m2 = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != (i % 2 == 0 ?'0' : '1')) m1++;
if (s.charAt(i) != (i % 2 == 0 ?'1' : '0')) m2++;
}
int res = Integer.MAX_VALUE;
if (c0 >= c1) res = Math.min(res, m1 / 2);
if (c1 >= c0) res = Math.min(res, m2 / 2);
return res == Integer.MAX_VALUE?-1 : res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funminSwaps(s: String): Int {
var c0 = 0; var c1 = 0for (ch in s) if (ch =='0') c0++else c1++if (kotlin.math.abs(c0 - c1) > 1) return -1var m1 = 0; var m2 = 0for (i in s.indices) {
if (s[i] !=if (i % 2==0) '0'else'1') m1++if (s[i] !=if (i % 2==0) '1'else'0') m2++ }
var res = Int.MAX_VALUE
if (c0 >= c1) res = minOf(res, m1 / 2)
if (c1 >= c0) res = minOf(res, m2 / 2)
returnif (res ==Int.MAX_VALUE) -1else res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defminSwaps(self, s: str) -> int:
c0, c1 = s.count('0'), s.count('1')
if abs(c0 - c1) >1:
return-1 m1 = sum(s[i] != str(i %2) for i in range(len(s)))
m2 = sum(s[i] != str(1- i %2) for i in range(len(s)))
res = float('inf')
if c0 >= c1:
res = min(res, m1 //2)
if c1 >= c0:
res = min(res, m2 //2)
return-1if res == float('inf') else res
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnmin_swaps(s: String) -> i32 {
let c0 = s.chars().filter(|&c| c =='0').count() asi32;
let c1 = s.chars().filter(|&c| c =='1').count() asi32;
if (c0 - c1).abs() >1 { return-1; }
let m1 = s.chars().enumerate().filter(|(i, c)|*c !=if i %2==0 { '0' } else { '1' }).count() asi32;
let m2 = s.chars().enumerate().filter(|(i, c)|*c !=if i %2==0 { '1' } else { '0' }).count() asi32;
letmut res =i32::MAX;
if c0 >= c1 { res = res.min(m1 /2); }
if c1 >= c0 { res = res.min(m2 /2); }
if res ==i32::MAX { -1 } else { res }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
minSwaps(s: string):number {
constc0= [...s].filter(c=>c==='0').length;
constc1= [...s].filter(c=>c==='1').length;
if (Math.abs(c0-c1) >1) return-1;
letm1=0, m2=0;
for (leti=0; i<s.length; ++i) {
if (s[i] !== (i%2===0?'0':'1')) m1++;
if (s[i] !== (i%2===0?'1':'0')) m2++;
}
letres=Infinity;
if (c0>=c1) res= Math.min(res, Math.floor(m1/2));
if (c1>=c0) res= Math.min(res, Math.floor(m2/2));
returnres===Infinity?-1 : res;
}
}