Minimize removal of substring of 0s to remove all occurrences of 0s from a circular Binary String
Last Updated :
21 May, 2021
Given circular binary string S of size N, the task is to count the minimum number of consecutive 0s required to be removed such that the string contains only 1s.
A circular string is a string whose first and last characters are considered to be adjacent to each other.
Examples:
Input: S = “11010001”
Output: 2
Explanation:
Remove the substring {S[2]}. Now, the string modifies to “1110001”.
Remove the substring {S[3], …, S[5]} of consecutive 0s. Now, the string modifies to “1111”.
Therefore, the minimum count of removals required is 2.
Input: S = “00110000”
Output: 1
Approach: The idea to solve the given problem is to traverse the given string and count the number of substrings having the same number of 0s, say C. Now if the first and the last characters of the string are ‘0’, then print the value of (C – 1) as the minimum number of removals required. Otherwise, print the value of C as the result.
Note: If the given string contains all 0s, then the minimum number of removals required is 1. Consider this case separately.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minRemovals(string str, int N)
{
int ans = 0;
bool X = false ;
for ( int i = 0; i < N; i++) {
if (str[i] == '0' ) {
ans++;
while (str[i] == '0' ) {
i++;
}
}
else {
X = true ;
}
}
if (!X)
return 1;
if (str[0] == '0'
and str[N - 1] == '0' ) {
ans--;
}
return ans;
}
int main()
{
string S = "11010001" ;
int N = S.size();
cout << minRemovals(S, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int minRemovals(String str, int N)
{
int ans = 0 ;
boolean X = false ;
for ( int i = 0 ; i < N; i++)
{
if (str.charAt(i) == '0' )
{
ans++;
while (i < N && str.charAt(i) == '0' )
{
i++;
}
}
else
{
X = true ;
}
}
if (!X)
return 1 ;
if (str.charAt( 0 ) == '0' &&
str.charAt(N - 1 ) == '0' )
{
ans--;
}
return ans;
}
public static void main(String[] args)
{
String S = "11010001" ;
int N = S.length();
System.out.println(minRemovals(S, N));
}
}
|
Python3
def minRemovals( str , N):
ans = 0
X = False
i = 0
while i < N:
if ( str [i] = = '0' ):
ans + = 1
while ( str [i] = = '0' ):
i + = 1
else :
X = True
i + = 1
if ( not X):
return 1
if ( str [ 0 ] = = '0' and str [N - 1 ] = = '0' ):
ans - = 1
return ans
S = "11010001"
N = len (S)
print (minRemovals(S, N))
|
C#
using System;
class GFG {
static int minRemovals( string str, int N)
{
int ans = 0;
bool X = false ;
for ( int i = 0; i < N; i++) {
if (str[i] == '0' ) {
ans++;
while (str[i] == '0' ) {
i++;
}
}
else {
X = true ;
}
}
if (!X)
return 1;
if (str[0] == '0' && str[N - 1] == '0' ) {
ans--;
}
return ans;
}
public static void Main()
{
string S = "11010001" ;
int N = S.Length;
Console.Write(minRemovals(S, N));
}
}
|
Javascript
<script>
function minRemovals(str, N)
{
let ans = 0;
let X = false ;
for (i = 0; i < N; i++) {
if (str[i] == '0' ) {
ans++;
while (str[i] == '0' ) {
i++;
}
}
else {
X = true ;
}
}
if (!X)
return 1;
if (str[0] == '0' && str[N - 1] == '0' ) {
ans--;
}
return ans;
}
let S = "11010001" ;
let N = S.length;
document.write(minRemovals(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...