📜  将二进制字符串的一半分开1和0

📅  最后修改于: 2021-04-17 18:12:55             🧑  作者: Mango

给定长度为偶数的二进制字符串str ,由相等的0 s和1 s组成,任务是通过重复反转子字符串将所有1和0分成单独的两半。打印所需的最少数量的冲销。

例子:

方法:想法是计算字符串的任意两个连续字符不相等的实例数。请按照以下步骤解决问题:

  • 初始化一个变量,例如ans ,以计算相邻的不相等字符对的数量。
  • 现在,在反转任何子字符串之后,计数减少2
  • 如果ans的值是奇数,则所需的答案将是(ans – 1)/ 2,因为最后的字符串将在字符串的中心包含一对这样的对。
  • 否则,所需答案为ans / 2

下面是上述方法的实现:

C++14
#include 
using namespace std;
 
// Function to count the minimum number
// of operations required to segregate
// all 1s and 0s in a binary string
void minOps(string s, int N)
{
   
    // Stores the count of unequal
    // pair of adjacent charcaters
    int ans = 0;
    for (int i = 1; i < N; i++)
    {
 
        // If an unequal pair of
        // adjacent characters occurs
        if (s[i] != s[i - 1])
        {
            ans++;
        }
    }
 
    // For odd count
    if (ans % 2 == 1)
    {
        cout << (ans - 1) / 2 << endl;
        return;
    }
 
    // For even count
    cout<<(ans / 2);
}
 
// Driver Code
int main()
{
   
  // Given string
  string str = "01011100";
 
  // Length of the string
  int N = str.size();
 
  // Prints the minimum count
  // of operations required
  minOps(str, N);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to count the minimum number
    // of operations required to segregate
    // all 1s and 0s in a binary string
    static void minOps(String s, int N)
    {
        // Stores the count of unequal
        // pair of adjacent charcaters
        int ans = 0;
 
        for (int i = 1; i < N; i++) {
 
            // If an unequal pair of
            // adjacent characters occurs
            if (s.charAt(i) != s.charAt(i - 1)) {
                ans++;
            }
        }
 
        // For odd count
        if (ans % 2 == 1) {
            System.out.print((ans - 1) / 2);
            return;
        }
 
        // For even count
        System.out.print(ans / 2);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given string
        String str = "01011100";
 
        // Length of the string
        int N = str.length();
 
        // Prints the minimum count
        // of operations required
        minOps(str, N);
    }
}


Python3
# Python 3 implementation of above approach
 
# Function to count the minimum number
# of operations required to segregate
# all 1s and 0s in a binary string
def minOps(s, N) :
   
    # Stores the count of unequal
    # pair of adjacent charcaters
    ans = 0
    for i in range(1, N):
 
        # If an unequal pair of
        # adjacent characters occurs
        if (s[i] != s[i - 1]) :
            ans += 1
         
    # For odd count
    if (ans % 2 == 1) :     
        print((ans - 1) // 2)
        return
     
    # For even count
    print(ans // 2)
 
# Driver Code
 
# Given string
str = "01011100"
 
# Length of the string
N = len(str)
 
# Prints the minimum count
# of operations required
minOps(str, N)
 
# This code is contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
public class GFG
{
 
    // Function to count the minimum number
    // of operations required to segregate
    // all 1s and 0s in a binary string
    static void minOps(String s, int N)
    {
       
        // Stores the count of unequal
        // pair of adjacent charcaters
        int ans = 0;
 
        for (int i = 1; i < N; i++)
        {
 
            // If an unequal pair of
            // adjacent characters occurs
            if (s[i] != s[i - 1])
            {
                ans++;
            }
        }
 
        // For odd count
        if (ans % 2 == 1)
        {
            Console.Write((ans - 1) / 2);
            return;
        }
 
        // For even count
        Console.Write(ans / 2);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
       
        // Given string
        String str = "01011100";
 
        // Length of the string
        int N = str.Length;
 
        // Prints the minimum count
        // of operations required
        minOps(str, N);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(1)