📌  相关文章
📜  使二进制字符串交替所需的最小交换

📅  最后修改于: 2021-10-26 05:14:03             🧑  作者: Mango

给定一个偶数长度的二进制字符串,其中 0 和 1 的数量相等。使字符串交替的最小交换次数是多少。如果没有两个连续元素相等,则二进制字符串是交替的。
例子:

Input : 000111
Output : 1
Explanation : Swap index 2 and index 5 to get 010101

Input : 1010
Output : 0

我们可能在第一个位置得到 1 或在第一个位置得到零。我们考虑两种情况并找到两种情况中的最小值。请注意,假设字符串的 1 和 0 的数量相等,并且字符串的长度是偶数。
1. 计算字符串奇数位置和偶数位置的零个数。让它们的计数分别为odd_0 和even_0。
2. 计算字符串奇数和偶数位置的个数。让它们的计数分别为odd_1 和even_1。
3. 我们总是将 1 与 0 交换(永远不会将 1 与 1 或 0 与 0 交换)。所以我们只检查我们的交替字符串是否以 0 开头,则交换次数为 min(even_0,odd_1),如果我们的交替字符串以 1 开头,则交换次数为 min(even_1,odd_0)。答案是这两个中的 min 。
下面是上述方法的实现:

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


Javascript


C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// function to count minimum swaps
// required to make binary string
// alternating
int countMinSwaps(string s)
{
    int N = s.size();
 
    // stores total number of ones
    int one = 0;
 
    // stores total number of zeroes
    int zero = 0;
 
    for (int i = 0; i < N; i++) {
        if (s[i] == '1')
            one++;
        else
            zero++;
    }
 
    // checking impossible condition
    if (one > zero + 1 || zero > one + 1)
        return -1;
 
    // odd length string
    if (N % 2) {
        // number of even positions
        int num = (N + 1) / 2;
 
        // stores number of zeroes and
        // ones at even positions
        int one_even = 0, zero_even = 0;
 
        for (int i = 0; i < N; i++) {
            if (i % 2 == 0) {
                if (s[i] == '1')
                    one_even++;
                else
                    zero_even++;
            }
        }
 
        if (one > zero)
            return num - one_even;
 
        else
            return num - zero_even;
    }
 
    // even length string
    else {
        // stores number of ones at odd
        // and even position respectively
        int one_odd = 0, one_even = 0;
 
        for (int i = 0; i < N; i++) {
            if (s[i] == '1') {
                if (i % 2)
                    one_odd++;
 
                else
                    one_even++;
            }
        }
 
        return min(N / 2 - one_odd, N / 2 - one_even);
    }
}
 
// Driver code
int main()
{
    string s = "111000";
 
    cout << countMinSwaps(s);
 
    return 0;
}


输出:
1

奇偶长度字符串:

令字符串长度为N。

在这种方法中,我们将考虑三种情况:
1.当1的总数>零的总数+1或零的总数>1的总数+1时,答案是不可能的。
2. 字符串是偶数长度:
我们将计算奇数位置( one_odd )和偶数位置( one_even )的个数,然后答案是 min ( N/2 – one_odd ,N/2 – one_even )
3. 字符串是奇数长度:
这里我们考虑两种情况:
i) 1 的总数 > 0 的总数(然后我们将 1 放在偶数位置)所以,答案是( (N + 1) / 2 – 偶数位置的 1 数)。
ii) 零的总数 > 一的总数(然后我们在偶数位置放了零)所以,答案是((N + 1)/ 2 – 偶数位置的零数)。

C++

// C++ implementation of the above approach
#include 
using namespace std;
 
// function to count minimum swaps
// required to make binary string
// alternating
int countMinSwaps(string s)
{
    int N = s.size();
 
    // stores total number of ones
    int one = 0;
 
    // stores total number of zeroes
    int zero = 0;
 
    for (int i = 0; i < N; i++) {
        if (s[i] == '1')
            one++;
        else
            zero++;
    }
 
    // checking impossible condition
    if (one > zero + 1 || zero > one + 1)
        return -1;
 
    // odd length string
    if (N % 2) {
        // number of even positions
        int num = (N + 1) / 2;
 
        // stores number of zeroes and
        // ones at even positions
        int one_even = 0, zero_even = 0;
 
        for (int i = 0; i < N; i++) {
            if (i % 2 == 0) {
                if (s[i] == '1')
                    one_even++;
                else
                    zero_even++;
            }
        }
 
        if (one > zero)
            return num - one_even;
 
        else
            return num - zero_even;
    }
 
    // even length string
    else {
        // stores number of ones at odd
        // and even position respectively
        int one_odd = 0, one_even = 0;
 
        for (int i = 0; i < N; i++) {
            if (s[i] == '1') {
                if (i % 2)
                    one_odd++;
 
                else
                    one_even++;
            }
        }
 
        return min(N / 2 - one_odd, N / 2 - one_even);
    }
}
 
// Driver code
int main()
{
    string s = "111000";
 
    cout << countMinSwaps(s);
 
    return 0;
}
输出
1

时间复杂度: O(字符串长度)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程