📜  最小化需要删除的 0 数以最大化 1 的最长子串的长度

📅  最后修改于: 2021-09-03 13:51:06             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是找到需要从给定字符串S 中删除的最小0数以获得最长的1s子串。

例子:

方法:这个想法是在字符串找到最左边和最右边的索引1 ,然后计算它们之间存在的0的数量。最后,打印获得的计数值。请按照以下步骤解决问题:

  • 遍历字符串s找到1字符串中的第一个和最后发生和存储其指数中的变量,说,分别。
  • 使用变量i在范围[left, right] 上迭代,如果str[i] 的值等于0 ,则将count增加 1。
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the minimum number
// of 0s required to be removed to
// maximize the longest substring of 1s
void minNumZeros(string str)
{
    // Stores leftmost and rightmost
    // indices consisting of 1
    int left = INT_MAX, right = INT_MIN;
 
    // Stores the count of 0s
    // between left and right
    int count = 0;
 
    // Traverse the string str
    for (int i = 0; i < str.length(); i++) {
 
        // If the current character is 1
        if (str[i] == '1') {
 
            // Update leftmost and rightmost
            // index consisting of 1
            left = min(i, left);
            right = max(right, i);
        }
    }
 
    // If string consists only of 0s
    if (left == INT_MAX) {
        cout << "0";
        return;
    }
 
    // Count the number of 0s
    // between left and right
    for (int i = left; i < right; i++) {
 
        if (str[i] == '0') {
            count++;
        }
    }
 
    // Print the result
    cout << count;
}
 
// Driver Code
int main()
{
    string str = "010011";
    minNumZeros(str);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to count the minimum number
// of 0s required to be removed to
// maximize the longest substring of 1s
static void minNumZeros(String str)
{
     
    // Stores leftmost and rightmost
    // indices consisting of 1
    int left = Integer.MAX_VALUE;
    int right = Integer.MIN_VALUE;
 
    // Stores the count of 0s
    // between left and right
    int count = 0;
 
    // Traverse the string str
    for(int i = 0; i < str.length(); i++)
    {
         
        // If the current character is 1
        if (str.charAt(i) == '1')
        {
             
            // Update leftmost and rightmost
            // index consisting of 1
            left = Math.min(i, left);
            right = Math.max(right, i);
        }
    }
 
    // If string consists only of 0s
    if (left == Integer.MAX_VALUE)
    {
        System.out.print("0");
        return;
    }
 
    // Count the number of 0s
    // between left and right
    for(int i = left; i < right; i++)
    {
        if (str.charAt(i) == '0')
        {
            count++;
        }
    }
 
    // Print the result
    System.out.print(count);
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "010011";
     
    minNumZeros(str);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program for the above approach
import sys
 
# Function to count the minimum number
# of 0s required to be removed to
# maximize the longest substring of 1s
def minNumZeros(st) :
 
    # Stores leftmost and rightmost
    # indices consisting of 1
    left = sys.maxsize
    right = -sys.maxsize - 1
 
    # Stores the count of 0s
    # between left and right
    count = 0
 
    # Traverse the string str
    for i in range(len(st)) :
    #for (int i = 0; i < str.length(); i++) {
 
        # If the current character is 1
        if st[i] == '1' :
 
            # Update leftmost and rightmost
            # index consisting of 1
            left = min(i, left)
            right = max(right, i)
         
    # If string consists only of 0s
    if left == sys.maxsize :
        print("0")
        return
     
    # Count the number of 0s
    # between left and right
    for i in range(left,right):
         
    #for (int i = left; i < right; i++) {
 
        if st[i] == '0' :
            count += 1
         
    # Print the result
    print(count)
 
# Driver Code
if __name__ == "__main__" :    
    st = "010011";
    minNumZeros(st)
     
 # This code is contributed by jana_sayantan.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the minimum number
// of 0s required to be removed to
// maximize the longest substring of 1s
static void minNumZeros(string str)
{
     
    // Stores leftmost and rightmost
    // indices consisting of 1
    int left = int.MaxValue;
    int right = int.MinValue;
 
    // Stores the count of 0s
    // between left and right
    int count = 0;
 
    // Traverse the string str
    for(int i = 0; i < str.Length; i++)
    {
         
        // If the current character is 1
        if (str[i] == '1')
        {
             
            // Update leftmost and rightmost
            // index consisting of 1
            left = Math.Min(i, left);
            right = Math.Max(right, i);
        }
    }
 
    // If string consists only of 0s
    if (left == int.MaxValue)
    {
        Console.WriteLine("0");
        return;
    }
 
    // Count the number of 0s
    // between left and right
    for(int i = left; i < right; i++)
    {
        if (str[i] == '0')
        {
            count++;
        }
    }
 
    // Print the result
    Console.WriteLine(count);
}
 
// Driver Code
static public void Main()
{
    string str = "010011";
     
    minNumZeros(str);
}
}
 
// This code is contributed by Dharanendra L V


输出:
2

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

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