📌  相关文章
📜  来自二进制字符串中除去毕竟(“10”或“01”)打印的字符串

📅  最后修改于: 2021-09-06 06:28:11             🧑  作者: Mango

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

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

因此,从上面的例子可以看出,只要字符串中有 1 和 0,就可以减少字符串。
我们已经在上一篇文章中讨论了查找 0 和 1 删除次数的方法。在这里,我们对之前的方法做了一些修改,以在所有可能的删除之后生成剩余的字符串。
方法:从上面的观察可以得出结论,最终的字符串只包含额外的 1 或 0 ,这些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


Javascript


输出:
00000

时间复杂度分析:

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live