📌  相关文章
📜  在二进制字符串中将所有0置于1之前所需的最少清除

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

给定二进制字符串S ,任务是找到需要从S删除的最小字符数,以使所有0都放在1 s之前。

例子:

方法:可以通过找到需要从右侧删除的最小‘0’个字符(例如right_0)和需要从左侧删除的最小‘1’个字符(例如left_1 )来解决该问题。获取所需的字符串。对于任何索引获得的right_0left_0的最小总和给出最终结果。

请按照以下步骤解决给定的问题:

  1. 初始化两个计数器变量,例如right_0left_1
  2. ‘0’的计数存储在right_0的字符串S中,并将left_1设置为0
  3. 初始化一个变量,例如res,以存储所需的答案。
  4. 遍历字符串S的字符以及每个字符:
    • 检查s [i]是否等于“ 0”。
      • 如果发现是真的,则将right_0减少1
      • 否则,将left_1增加1
    • 检查right_0,left_1之和是否小于res 。如果发现属实,那么更新资源等于最小资源right_0 + left_1
  5. 遍历数组后,将res打印为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count minimum removals
// required to arrange all 0s before 1s
int minimumDeletions(string s)
{
    // Count the occurences of 0 in s
    int right_0 = count(s.begin(), s.end(), '0');
 
    int left_1 = 0;
     
    // Size of the string
    int n = s.size();
     
    // Stores the minimum
    // number of removals required
    int res = INT_MAX;
     
    // Iterate over each of the
    // characters in the string s
    for (int i = 0; i < n; i++)
    {
        // If the i-th character
        // is found to be '0'
        if (s[i] == '0')
        {
            right_0 -= 1;
        }
        else
        {
            left_1 += 1;
        }
         
        // Store the minimum of res
        // and right_0 + left_1 in res
        res = min(res, right_0 + left_1);
    }
   
    // Return the final result
    return res;
}
 
 
// Driver Code
int main()
{
    string s = "001101";
    int count = minimumDeletions(s);
     
    cout << count;
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
     
// Function to count minimum removals
// required to arrange all 0s before 1s
static int minimumDeletions(String s)
{
   
    // Count the occurences of 0 in s
    int right_0 = (int)(s.chars().filter(ch -> ch == '0').count());
    int left_1 = 0;
     
    // Size of the string
    int n = s.length();
     
    // Stores the minimum
    // number of removals required
    int res = Integer.MAX_VALUE;
     
    // Iterate over each of the
    // characters in the string s
    for (int i = 0; i < n; i++)
    {
        // If the i-th character
        // is found to be '0'
        if (s.charAt(i) == '0')
        {
            right_0 -= 1;
        }
        else
        {
            left_1 += 1;
        }
         
        // Store the minimum of res
        // and right_0 + left_1 in res
        res = Math.min(res, right_0 + left_1);
    }
   
    // Return the final result
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "001101";
    int count = minimumDeletions(s);
     
    System.out.print(count);
}
}
 
// This code is contributed by  sanjoy_62.


Python3
# Python3 program for the above approach
import sys
 
# Function to count minimum removals
# required to arrange all 0s before 1s
def minimumDeletions(s) :
     
    # Count the occurences of 0 in s
    right_0 = s.count('0')
 
    left_1 = 0
     
    # Size of the string
    n = len(s)
     
    # Stores the minimum
    # number of removals required
    res = sys.maxsize
     
    # Iterate over each of the
    # characters in the string s
    for i in range(n):
         
        # If the i-th character
        # is found to be '0'
        if (s[i] == '0') :
            right_0 -= 1
         
        else :
            left_1 += 1
         
        # Store the minimum of res
        # and right_0 + left_1 in res
        res = min(res, right_0 + left_1)
     
    # Return the final result
    return res
 
# Driver Code
s = "001101"
count = minimumDeletions(s)
     
print( count)
 
# This code is contributed by splevel62.


C#
// C# program for above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to count minimum removals
// required to arrange all 0s before 1s
static int minimumDeletions(string s)
{
   
    // Count the occurences of 0 in s
    int right_0 = (int)s.Split('0').Length - 1;
    int left_1 = 0;
     
    // Size of the string
    int n = s.Length;
     
    // Stores the minimum
    // number of removals required
    int res = Int32.MaxValue;
     
    // Iterate over each of the
    // characters in the string s
    for (int i = 0; i < n; i++)
    {
        // If the i-th character
        // is found to be '0'
        if (s[i] == '0')
        {
            right_0 -= 1;
        }
        else
        {
            left_1 += 1;
        }
         
        // Store the minimum of res
        // and right_0 + left_1 in res
        res = Math.Min(res, right_0 + left_1);
    }
   
    // Return the final result
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    string s = "001101";
    int count = minimumDeletions(s);
     
    Console.WriteLine(count);
}
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
1

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