📌  相关文章
📜  将所有 0 放在二进制字符串中的 1 之前所需的最小删除

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

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

例子:

方法:求出右边需要删除的‘0’字符的最小个数,比如right_0,以及左边需要删除的‘1’字符的最小个数,比如left_1 ,可以解决这个问题。获取所需的字符串。对于任何索引获得的right_0left_0的最小总和给出了最终结果。

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

  1. 初始化两个计数器变量,比如right_0left_1
  2. 将字符串S‘0’的计数存储在right_0 中,并将left_1设置为0
  3. 初始化一个变量,比如res,以存储所需的答案。
  4. 迭代字符串S的字符和每个字符:
    • 检查s[i] 是否等于“0”。
      • 如果发现为真,则将right_0 减1
      • 否则,将left_1增加1
    • 检查和right_0, left_1是否小于res 。如果发现为真,则更新res等于resright_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.


Javascript


输出:
1

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

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