📌  相关文章
📜  最小化删除 0 的子串以从循环二进制字符串中删除所有出现的 0

📅  最后修改于: 2021-09-03 14:33:21             🧑  作者: Mango

给定大小为N 的循环二进制字符串S ,任务是计算需要删除的连续0的最小数量,以便该字符串仅包含1

例子:

方法:解决给定问题的想法是遍历给定的字符串并计算具有相同数量0的子字符串的数量,例如C 。现在,如果字符串的第一个和最后一个字符是‘0’ ,则打印(C – 1)的值作为所需的最小删除次数。否则,打印C的值作为结果。

注意:如果给定的字符串包含全0 ,则所需的最小删除次数为1 。单独考虑这种情况。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count minimum number of
// removal of consecutive 0s required to
// make binary string consists only of 1s
int minRemovals(string str, int N)
{
    // Stores the count of removals
    int ans = 0;
 
    bool X = false;
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // If the current character is '0'
        if (str[i] == '0') {
 
            ans++;
 
            // Traverse until consecutive
            // characters are only '0's
            while (str[i] == '0') {
                i++;
            }
        }
 
        else {
            X = true;
        }
    }
 
    // If the binary string only
    // contains 1s, then return 1
    if (!X)
        return 1;
 
    // If the first and the last
    // characters are 0
    if (str[0] == '0'
        and str[N - 1] == '0') {
        ans--;
    }
 
    // Return the resultant count
    return ans;
}
 
// Driver Code
int main()
{
    string S = "11010001";
    int N = S.size();
    cout << minRemovals(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to count minimum number of
// removal of consecutive 0s required to
// make binary string consists only of 1s
static int minRemovals(String str, int N)
{
     
    // Stores the count of removals
    int ans = 0;
 
    boolean X = false;
 
    // Traverse the string S
    for(int i = 0; i < N; i++)
    {
         
        // If the current character is '0'
        if (str.charAt(i) == '0')
        {
            ans++;
 
            // Traverse until consecutive
            // characters are only '0's
            while (i < N && str.charAt(i) == '0')
            {
                i++;
            }
        }
 
        else
        {
            X = true;
        }
    }
 
    // If the binary string only
    // contains 1s, then return 1
    if (!X)
        return 1;
 
    // If the first and the last
    // characters are 0
    if (str.charAt(0) == '0' &&
        str.charAt(N - 1) == '0')
    {
        ans--;
    }
 
    // Return the resultant count
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "11010001";
    int N = S.length();
 
    System.out.println(minRemovals(S, N));
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to count minimum number of
# removal of consecutive 0s required to
# make binary string consists only of 1s
def minRemovals(str, N):
     
    # Stores the count of removals
    ans = 0
    X = False
     
    # Traverse the string S
    i = 0
     
    while i < N:
         
        # If the current character is '0'
        if (str[i] == '0'):
            ans += 1
             
            # Traverse until consecutive
            # characters are only '0's
            while (str[i] == '0'):
                i += 1
        else:
            X = True
             
        i += 1
         
    # If the binary string only
    # contains 1s, then return 1
    if (not X):
        return 1
         
    # If the first and the last
    # characters are 0
    if (str[0] == '0' and str[N - 1] == '0'):
        ans -= 1
         
    # Return the resultant count
    return ans
     
# Driver Code
S = "11010001"
N = len(S)
 
print(minRemovals(S, N))
 
# This code is contributed by rohan07


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to count minimum number of
  // removal of consecutive 0s required to
  // make binary string consists only of 1s
  static int minRemovals(string str, int N)
  {
     
    // Stores the count of removals
    int ans = 0;
 
    bool X = false;
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
      // If the current character is '0'
      if (str[i] == '0') {
 
        ans++;
 
        // Traverse until consecutive
        // characters are only '0's
        while (str[i] == '0') {
          i++;
        }
      }
 
      else {
        X = true;
      }
    }
 
    // If the binary string only
    // contains 1s, then return 1
    if (!X)
      return 1;
 
    // If the first and the last
    // characters are 0
    if (str[0] == '0' && str[N - 1] == '0') {
      ans--;
    }
 
    // Return the resultant count
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    string S = "11010001";
    int N = S.Length;
    Console.Write(minRemovals(S, N));
  }
}
 
// This code is contributed by subham348.


Javascript


输出:
2

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

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