 Open in App
Not now

# Minimize removal of alternating subsequences to empty given Binary String

• Difficulty Level : Expert
• Last Updated : 04 May, 2021

Given a binary string S of length N, the task is to minimize the count of repetitive removal of alternating subsequence of the 0 and 1 from the given binary string S to make the string empty.

Examples:

Input: S = “0100100111”
Output: 3
Explanation:
Remove subsequence “010101” from S to modify it to “0011”.
Remove “01” from “0011” to make it “01”.
Finally, remove “01” to make it an empty string.

Input: S = “1111”
Output: 4

Approach: The given problem can be solved by observing that an alternating subsequence of 0 and 1 is to be removed and to remove all the consecutive characters 1s or 0s can only be removed at each separate operation, not in a single operation.

Therefore, the minimum number of operations required is the maximum count of consecutive 0s and 1s.

Below is the implementation of the above approach:

## 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

 ``

Output:

`3`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up