📜  从二进制字符串删除所有字符串(“ 10”或“ 01”)后打印字符串

📅  最后修改于: 2021-04-29 09:08:01             🧑  作者: Mango

给定一个仅由0和1组成的二进制字符串str ,任务是在从字符串逐一删除出现的“ 10”和“ 01”后打印字符串。如果字符串空,则输出-1。

例子:

观察:在仔细观察,因为给定的字符串是一个二进制字符串,所有字符串可以除多余的0和1名的是目前无法得到与其配对的恭维字符串中被清除。例如:

因此,从上面的示例可以看出,只要字符串中有1和0,就可以减少字符串。

我们已经在上一篇文章中讨论了查找0和1的缺失计数的方法。在这里,我们对先前的方法做了一些修改,以在所有可能的删除之后生成剩余的字符串。

方法:根据以上观察结果,可以得出结论,最终的字符串仅包含多余的1或0 ,这些数字不能与字符串中的任何数字配对。因此,解决此问题的想法是对字符串中0和1的数目进行计数,然后找出两个计数之间的。此计数表示以较高的值为准,剩余的1或0

可以按照以下步骤计算答案:

  1. 获取字符串存在的0计数并将存储在变量中。
  2. 获取字符串存在的1的计数并将存储在另一个变量中。
  3. 如果1的计数等于0的计数,则可以减少整个字符串。因此,返回-1
  4. 如果1的个数大于0的个数,那么最后剩下的那许多1将被保留,无法进一步减少。因此,请将这许多1附加到一个空字符串,然后返回字符串。
  5. 同样,如果0的数量大于1的数量,则求出差值,然后将这多个0附加到空字符串并返回。

下面是上述方法的实现:

C++
// C++ program to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
#include 
using namespace std;
  
// Function to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
void finalString(string str)
{
  
    // Variables to store the
    // count of 1's and 0's
    int x = 0, y = 0;
  
    // Variable left will store
    // whether 0's or 1's is left
    // in the final string
    int left;
  
    // Length of the string
    int n = str.length();
  
    // For loop to count the occurrences
    // of 1's and 0's in the string
    for (int i = 0; i < n; i++) {
        if (str[i] == '1')
            x++;
        else
            y++;
    }
  
    // To check if the count of 1's is
    // greater than the count of 0's or not.
    // If x is greater, then those many 1's
    // are printed.
    if (x > y)
        left = 1;
    else
        left = 0;
  
    // Length of the final remaining string
    // after removing all the occurrences
    int length = n - 2 * min(x, y);
  
    // Printing the final string
    for (int i = 0; i < length; i++) {
        cout << left;
    }
}
  
// Driver Code
int main()
{
    string str = "010110100100000";
    finalString(str);
  
    return 0;
}


Java
// Java program to print the final String
// after removing all the occurrences of
// "10" and "01" from the given binary String
import java.util.*;
  
class GFG{
   
// Function to print the final String
// after removing all the occurrences of
// "10" and "01" from the given binary String
static void finalString(String str)
{
   
    // Variables to store the
    // count of 1's and 0's
    int x = 0, y = 0;
   
    // Variable left will store
    // whether 0's or 1's is left
    // in the final String
    int left;
   
    // Length of the String
    int n = str.length();
   
    // For loop to count the occurrences
    // of 1's and 0's in the String
    for (int i = 0; i < n; i++) {
        if (str.charAt(i) == '1')
            x++;
        else
            y++;
    }
   
    // To check if the count of 1's is
    // greater than the count of 0's or not.
    // If x is greater, then those many 1's
    // are printed.
    if (x > y)
        left = 1;
    else
        left = 0;
   
    // Length of the final remaining String
    // after removing all the occurrences
    int length = n - 2 * Math.min(x, y);
   
    // Printing the final String
    for (int i = 0; i < length; i++) {
        System.out.print(left);
    }
}
   
// Driver Code
public static void main(String[] args)
{
    String str = "010110100100000";
    finalString(str);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python 3 program to print the final string
# after removing all the occurrences of
# "10" and "01" from the given binary string
  
# Function to print the final string
# after removing all the occurrences of
# "10" and "01" from the given binary string
def finalString(st):
  
    # Variables to store the
    # count of 1's and 0's
    x , y = 0 , 0
  
    # Length of the string
    n = len(st)
  
    # For loop to count the occurrences
    # of 1's and 0's in the string
    for i in range( n):
        if (st[i] == '1'):
            x += 1
        else:
            y += 1
  
    # To check if the count of 1's is
    # greater than the count of 0's or not.
    # If x is greater, then those many 1's
    # are printed.
    if (x > y):
        left = 1
    else:
        left = 0
  
    # Length of the final remaining string
    # after removing all the occurrences
    length = n - 2 * min(x, y);
  
    # Printing the final string
    for i in range(length):
        print(left, end="")
  
  
# Driver Code
if __name__ == "__main__":
    st = "010110100100000"
    finalString(st)
  
# This code is contributed by chitranayal


C#
// C# program to print the readonly String
// after removing all the occurrences of
// "10" and "01" from the given binary String
using System;
  
class GFG{
    
// Function to print the readonly String
// after removing all the occurrences of
// "10" and "01" from the given binary String
static void finalString(String str)
{
    
    // Variables to store the
    // count of 1's and 0's
    int x = 0, y = 0;
    
    // Variable left will store
    // whether 0's or 1's is left
    // in the readonly String
    int left;
    
    // Length of the String
    int n = str.Length;
    
    // For loop to count the occurrences
    // of 1's and 0's in the String
    for (int i = 0; i < n; i++) {
        if (str[i] == '1')
            x++;
        else
            y++;
    }
    
    // To check if the count of 1's is
    // greater than the count of 0's or not.
    // If x is greater, then those many 1's
    // are printed.
    if (x > y)
        left = 1;
    else
        left = 0;
    
    // Length of the readonly remaining String
    // after removing all the occurrences
    int length = n - 2 * Math.Min(x, y);
    
    // Printing the readonly String
    for (int i = 0; i < length; i++) {
        Console.Write(left);
    }
}
    
// Driver Code
public static void Main(String[] args)
{
    String str = "010110100100000";
    finalString(str);
}
}
   
// This code is contributed by 29AjayKumar


输出:
00000

时间复杂度分析:

  • for循环对1和0的出现次数进行计数需要O(N)时间,其中N是字符串的长度。
  • if语句需要固定的时间。因此,if语句的时间复杂度为O(1)
  • 在最坏的情况下,当整个字符串只有0或1时,打印最终字符串的循环将花费O(N)
  • 因此,总时间复杂度为O(N)