📌  相关文章
📜  最小化交替子序列的移除以清空给定的二进制字符串

📅  最后修改于: 2021-09-06 06:37:55             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是最小化从给定的二进制字符串S中重复去除01的交替子序列以使字符串空的次数。

例子:

方法:给定的问题可以通过观察01的交替子序列被删除来解决,并且删除所有连续字符10只能在每个单独的操作中被删除,而不是在单个操作中被删除。

因此,所需的最小操作次数是连续01的最大计数。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
void minOpsToEmptyString(string S, int N)
{
    // Initialize variables
    int one = 0, zero = 0;
 
    int x0 = 0, x1 = 0;
 
    // Traverse the string
    for (int i = 0; i < N; i++) {
 
        // If current character is 0
        if (S[i] == '0') {
            x0++;
            x1 = 0;
        }
        else {
            x1++;
            x0 = 0;
        }
 
        // Update maximum consecutive
        // 0s and 1s
        zero = max(x0, zero);
        one = max(x1, one);
    }
 
    // Print the minimum operation
    cout << max(one, zero) << endl;
}
 
// Driver code+
int main()
{
   
    // input string
    string S = "0100100111";
   
    // length of string
    int N = S.length();
   
    // Function Call
    minOpsToEmptyString(S, N);
}
 
// This code is contributed by aditya7409


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum
    // number of operations required
    // to empty the string
    public static void
    minOpsToEmptyString(String S,
                        int N)
    {
        // Initialize variables
        int one = 0, zero = 0;
 
        int x0 = 0, x1 = 0;
 
        // Traverse the string
        for (int i = 0; i < N; i++) {
 
            // If current character is 0
            if (S.charAt(i) == '0') {
                x0++;
                x1 = 0;
            }
            else {
                x1++;
                x0 = 0;
            }
 
            // Update maximum consecutive
            // 0s and 1s
            zero = Math.max(x0, zero);
            one = Math.max(x1, one);
        }
 
        // Print the minimum operation
        System.out.println(
            Math.max(one, zero));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "0100100111";
        int N = S.length();
 
        // Function Call
        minOpsToEmptyString(S, N);
    }
}


Python3
# Python3 program for the above approach
def minOpsToEmptyString(S, N):
 
    # Initialize variables
    one = 0
    zero = 0
    x0 = 0
    x1 = 0
 
    # Traverse the string
    for i in range(N):
 
        # If current character is 0
        if (S[i] == '0'):
            x0 += 1
            x1 = 0
        else:
            x1 += 1
            x0 = 0
 
        # Update maximum consecutive
        # 0s and 1s
        zero = max(x0, zero)
        one = max(x1, one)
 
    # Print the minimum operation
    print(max(one, zero))
 
# Driver code+
if __name__ == "__main__":
 
    # input string
    S = "0100100111"
 
    # length of string
    N = len(S)
 
    # Function Call
    minOpsToEmptyString(S, N)
 
    # This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to find the minimum
    // number of operations required
    // to empty the string
    public static void
      minOpsToEmptyString(string S, int N)
    {
        // Initialize variables
        int one = 0, zero = 0;
 
        int x0 = 0, x1 = 0;
 
        // Traverse the string
        for (int i = 0; i < N; i++)
        {
 
            // If current character is 0
            if (S[i] == '0')
            {
                x0++;
                x1 = 0;
            }
            else
            {
                x1++;
                x0 = 0;
            }
 
            // Update maximum consecutive
            // 0s and 1s
            zero = Math.Max(x0, zero);
            one = Math.Max(x1, one);
        }
 
        // Print the minimum operation
        Console.WriteLine(Math.Max(one, zero));
    }
 
    // Driver Code
    static public void Main()
    {
        string S = "0100100111";
        int N = S.Length;
 
        // Function Call
        minOpsToEmptyString(S, N);
    }
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
3

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

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