📌  相关文章
📜  要从给定的二进制字符串中删除以使所有 1 连续出现的最小 0 计数

📅  最后修改于: 2022-05-13 01:56:06.397000             🧑  作者: Mango

要从给定的二进制字符串中删除以使所有 1 连续出现的最小 0 计数

给定一个大小为N的二进制字符串S ,任务是找到必须从字符串S中删除的最小0数量,以使所有1连续出现。

例子:

方法:可以通过观察以下事实来解决给定的问题:删除字符串中的前导和结束0不会最小化必须删除的0的数量。因此,想法是在字符串S中找到第一次和最后一次出现的1 ,并找到它们之间的0的计数。请按照以下步骤解决问题:

  • 从左到右遍历字符串S的字符,并找到给定字符串X中第一次出现1的索引。
  • 从右到左遍历字符串,找到给定字符串Y中最后一次出现1的索引。
  • 完成上述步骤后,打印索引XY之间的0计数作为结果。
C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum count of
// 0s removed from the string S such
// that all 1s occurs consecutively
void makeContiguous(string S, int N)
{
    // Stores the first and the last
    // occurrence of 1
    int fst_occur, lst_occur;
 
    // Iterate over the characters
    // the string, S
    for (int x = 0; x < N; x++) {
 
        // If current character
        // is '1'
        if (S[x] == '1') {
 
            // Update fst_occur
            fst_occur = x;
            break;
        }
    }
 
    // Iterate over the characters
    // the string, S
    for (int x = N - 1; x >= 0; x--) {
 
        // If current character
        // is '1'
        if (S[x] == '1') {
 
            // Update lst_occur
            lst_occur = x;
            break;
        }
    }
 
    // Stores the count of 0s between
    // fst_occur and lst_occur
    int count = 0;
 
    // Iterate over the characters of S
    // between fst_occur and lst_occur
    for (int x = fst_occur;
        x <= lst_occur; x++) {
 
        // If current character is '0'
        if (S[x] == '0') {
 
            // Update count
            count++;
        }
    }
 
    // Print the resultant minimum count
    cout << count;
}
 
// Driver Code
int main()
{
    string S = "010001011";
    int N = S.size();
    makeContiguous(S, N);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find minimum count of
// 0s removed from the String S such
// that all 1s occurs consecutively
static void makeContiguous(String S, int N)
{
    // Stores the first and the last
    // occurrence of 1
    int fst_occur=0, lst_occur=0;
 
    // Iterate over the characters
    // the String, S
    for (int x = 0; x < N; x++) {
 
        // If current character
        // is '1'
        if (S.charAt(x) == '1') {
 
            // Update fst_occur
            fst_occur = x;
            break;
        }
    }
 
    // Iterate over the characters
    // the String, S
    for (int x = N - 1; x >= 0; x--) {
 
        // If current character
        // is '1'
        if (S.charAt(x) == '1') {
 
            // Update lst_occur
            lst_occur = x;
            break;
        }
    }
 
    // Stores the count of 0s between
    // fst_occur and lst_occur
    int count = 0;
 
    // Iterate over the characters of S
    // between fst_occur and lst_occur
    for (int x = fst_occur; x <= lst_occur; x++) {
 
        // If current character is '0'
        if (S.charAt(x) == '0') {
 
            // Update count
            count++;
        }
    }
 
    // Print the resultant minimum count
    System.out.print(count);
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "010001011";
    int N = S.length();
    makeContiguous(S, N);
 
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function to find minimum count of
# 0s removed from the string S such
# that all 1s occurs consecutively
def makeContiguous(S, N):
   
    # Stores the first and the last
    # occurrence of 1
    fst_occur = 0
    lst_occur = 0
 
    # Iterate over the characters
    # the string, S
    for x in range(N):
       
        # If current character
        # is '1'
        if (S[x] == '1'):
           
            # Update fst_occur
            fst_occur = x
            break
 
    # Iterate over the characters
    # the string, S
    x = N - 1
    while(x >= 0):
       
        # If current character
        # is '1'
        if (S[x] == '1'):
           
            # Update lst_occur
            lst_occur = x
            break
        x -= 1
 
    # Stores the count of 0s between
    # fst_occur and lst_occur
    count = 0
 
    # Iterate over the characters of S
    # between fst_occur and lst_occur
    for x in range(fst_occur,lst_occur+1,1):
        # If current character is '0'
        if (S[x] == '0'):
            # Update count
            count += 1
 
    # Print the resultant minimum count
    print(count)
 
# Driver Code
if __name__ == '__main__':
    S = "010001011"
    N = len(S)
    makeContiguous(S, N)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find minimum count of
// 0s removed from the string S such
// that all 1s occurs consecutively
static void makeContiguous(string S, int N)
{
     
    // Stores the first and the last
    // occurrence of 1
    int fst_occur = 0, lst_occur = 0;
 
    // Iterate over the characters
    // the string, S
    for(int x = 0; x < N; x++)
    {
         
        // If current character
        // is '1'
        if (S[x] == '1')
        {
             
            // Update fst_occur
            fst_occur = x;
            break;
        }
    }
 
    // Iterate over the characters
    // the string, S
    for(int x = N - 1; x >= 0; x--)
    {
         
        // If current character
        // is '1'
        if (S[x] == '1')
        {
             
            // Update lst_occur
            lst_occur = x;
            break;
        }
    }
 
    // Stores the count of 0s between
    // fst_occur and lst_occur
    int count = 0;
 
    // Iterate over the characters of S
    // between fst_occur and lst_occur
    for(int x = fst_occur; x <= lst_occur; x++)
    {
         
        // If current character is '0'
        if (S[x] == '0')
        {
             
            // Update count
            count++;
        }
    }
 
    // Print the resultant minimum count
    Console.Write(count);
}
 
// Driver Code
public static void Main()
{
    string S = "010001011";
    int N = S.Length;
     
    makeContiguous(S, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出
4