系统会为您提供一个长度为偶数且等于0和1的二进制字符串。使字符串交替的最小交换次数是多少?如果没有两个连续的元素相等,则二进制字符串是交替的。
例子:
Input : 000111
Output : 1
Explanation : Swap index 2 and index 5 to get 010101
Input : 1010
Output : 0
我们可能在第一位置获得1或在第一位置获得零。我们考虑两种情况,并找到两种情况中的最小值。注意,假定字符串的数字1和0相等,并且字符串的长度为偶数。
1.在字符串的奇数位置和偶数位置计数零的数目。令它们的计数分别为奇数_0和偶数_0。
2.计算字符串的奇数位和偶数位的个数。令它们的计数分别为奇数_1和偶数_1。
3.我们将始终将1与0交换(永远不要将1与1交换或将0与0交换)。因此,我们只需检查交替字符串是否以0开头,则交换数为min(even_0,奇数_1),如果交替字符串以1开头,则交换数为min(even_1,奇数_0)。答案是这两个中的最小值。
下面是上述方法的实现:
C++
// CPP implementation of the approach
#include
using namespace std;
// returns the minimum number of swaps
// of a binary string
// passed as the argument
// to make it alternating
int countMinSwaps(string st)
{
int min_swaps = 0;
// counts number of zeroes at odd
// and even positions
int odd_0 = 0, even_0 = 0;
// counts number of ones at odd
// and even positions
int odd_1 = 0, even_1 = 0;
int n = st.length();
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (st[i] == '1')
even_1++;
else
even_0++;
}
else {
if (st[i] == '1')
odd_1++;
else
odd_0++;
}
}
// alternating string starts with 0
int cnt_swaps_1 = min(even_0, odd_1);
// alternating string starts with 1
int cnt_swaps_2 = min(even_1, odd_0);
// calculates the minimum number of swaps
return min(cnt_swaps_1, cnt_swaps_2);
}
// Driver code
int main()
{
string st = "000111";
cout<
Java
// Java implementation of the approach
class GFG {
// returns the minimum number of swaps
// of a binary string
// passed as the argument
// to make it alternating
static int countMinSwaps(String st)
{
int min_swaps = 0;
// counts number of zeroes at odd
// and even positions
int odd_0 = 0, even_0 = 0;
// counts number of ones at odd
// and even positions
int odd_1 = 0, even_1 = 0;
int n = st.length();
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (st.charAt(i) == '1')
even_1++;
else
even_0++;
}
else {
if (st.charAt(i) == '1')
odd_1++;
else
odd_0++;
}
}
// alternating string starts with 0
int cnt_swaps_1 = Math.min(even_0, odd_1);
// alternating string starts with 1
int cnt_swaps_2 = Math.min(even_1, odd_0);
// calculates the minimum number of swaps
return Math.min(cnt_swaps_1, cnt_swaps_2);
}
// Driver code
public static void main(String[] args)
{
String st = "000111";
System.out.println(countMinSwaps(st));
}
}
Python 3
# Python3 implementation of the
# above approach
# returns the minimum number of swaps
# of a binary string
# passed as the argument
# to make it alternating
def countMinSwaps(st) :
min_swaps = 0
# counts number of zeroes at odd
# and even positions
odd_0, even_0 = 0, 0
# counts number of ones at odd
# and even positions
odd_1, even_1 = 0, 0
n = len(st)
for i in range(0, n) :
if i % 2 == 0 :
if st[i] == "1" :
even_1 += 1
else :
even_0 += 1
else :
if st[i] == "1" :
odd_1 += 1
else :
odd_0 += 1
# alternating string starts with 0
cnt_swaps_1 = min(even_0, odd_1)
# alternating string starts with 1
cnt_swaps_2 = min(even_1, odd_0)
# calculates the minimum number of swaps
return min(cnt_swaps_1, cnt_swaps_2)
# Driver code
if __name__ == "__main__" :
st = "000111"
# Function call
print(countMinSwaps(st))
# This code is contributed by
# ANKITRAI1
C#
// C# implementation of the approach
using System;
public class GFG
{
// returns the minimum number of swaps
// of a binary string
// passed as the argument
// to make it alternating
public static int countMinSwaps(string st)
{
int min_swaps = 0;
// counts number of zeroes at odd
// and even positions
int odd_0 = 0, even_0 = 0;
// counts number of ones at odd
// and even positions
int odd_1 = 0, even_1 = 0;
int n = st.Length;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (st[i] == '1')
{
even_1++;
}
else
{
even_0++;
}
}
else
{
if (st[i] == '1')
{
odd_1++;
}
else
{
odd_0++;
}
}
}
// alternating string starts with 0
int cnt_swaps_1 = Math.Min(even_0, odd_1);
// alternating string starts with 1
int cnt_swaps_2 = Math.Min(even_1, odd_0);
// calculates the minimum number of swaps
return Math.Min(cnt_swaps_1, cnt_swaps_2);
}
// Driver code
public static void Main(string[] args)
{
string st = "000111";
Console.WriteLine(countMinSwaps(st));
}
}
// This code is contributed by Shrikant13
PHP
输出:
1