Minimum substring reversals required to make given Binary String alternating
Last Updated :
01 Jun, 2021
Given a binary string S of length N, the task is to count the minimum number substrings of S that is required to be reversed to make the string S alternating. If it is not possible to make string alternating, then print “-1”.
Examples:
Input: S = “10001110”
Output: 2
Explanation:
In the first operation, reversing the substring {S[3], .., S[6]} modifies the string to “10110010”.
In the second operation, reversing the substring {S[4], .. S[5]}modifies the string to “10101010”, which is alternating.
Input: S = “100001”
Output: -1
Explanation: Not possible to obtain an alternating binary string.
Approach: The idea is based on the observation that when a substring s[L, R] is reversed, then no more than two pairs s[L – 1], s[L] and s[R], S[R + 1] are changed. Moreover, one pair should be a consecutive pair of 00 and the other 11. So, the minimum number of operations can be obtained by pairing 00 with 11 or with the left/right border of S. Thus, the required number of operations is half of the number of consecutive pairs of the same character. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumReverse(string s, int n)
{
int k = 0 , l = 0 ;
int sum1 = 0, sum0 = 0;
for ( int i = 1; i < n; i++) {
if (s[i] == '1' )
sum1++;
else
sum0++;
if (s[i] == s[i - 1]&& s[i] == '0' )
k++;
else if ( s[i] == s[i - 1]&& s[i] == '1' )
l++;
}
if (s[0]== '1' )
sum1++;
else
sum0++;
if ( abs (sum1 - sum0) > 1)
return -1;
return max(k , l );
}
int main()
{
string S = "10001" ;
int N = S.size();
cout << minimumReverse(S, N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int minimumReverse(String s, int n)
{
int k = 0 , l = 0 ;
int sum1 = 0 , sum0 = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (s.charAt(i) == '1' )
sum1++;
else
sum0++;
if (s.charAt(i) == s.charAt(i - 1 ) && s.charAt(i) == '0' )
k++;
else if ( s.charAt(i) == s.charAt(i - 1 ) && s.charAt(i) == '1' )
l++;
}
if (s.charAt( 0 )== '1' )
sum1++;
else
sum0++;
if (Math.abs(sum1 - sum0) > 1 )
return - 1 ;
return Math.max(k , l);
}
public static void main (String[] args)
{
String S = "10001" ;
int N = S.length();
System.out.print(minimumReverse(S, N));
}
}
|
Python3
def minimumReverse(s, n):
k = 0 ;
l = 0 ;
sum1 = 0 ;
sum0 = 0 ;
for i in range ( 1 , n):
if (s[i] = = '1' ):
sum1 + = 1 ;
else :
sum0 + = 1 ;
if (s[i] = = s[i - 1 ] and s[i] = = '0' ):
k + = 1 ;
elif (s[i] = = s[i - 1 ] and s[i] = = '1' ):
l + = 1 ;
if (s[ 0 ] = = '1' ):
sum1 + = 1 ;
else :
sum0 + = 1 ;
if ( abs (sum1 - sum0) > 1 ):
return - 1 ;
return max (k, l);
if __name__ = = '__main__' :
S = "10001" ;
N = len (S);
print (minimumReverse(S, N));
|
C#
using System;
public class GFG
{
static int minimumReverse(String s, int n)
{
int k = 0 , l = 0 ;
int sum1 = 0, sum0 = 0;
for ( int i = 1; i < n; i++)
{
if (s[i] == '1' )
sum1++;
else
sum0++;
if (s[i] == s[i-1] && s[i] == '0' )
k++;
else if ( s[i] == s[i-1] && s[i] == '1' )
l++;
}
if (s[0] == '1' )
sum1++;
else
sum0++;
if (Math.Abs(sum1 - sum0) > 1)
return -1;
return Math.Max(k , l);
}
public static void Main(String[] args)
{
String S = "10001" ;
int N = S.Length;
Console.Write(minimumReverse(S, N));
}
}
|
Javascript
<script>
function minimumReverse(s, n)
{
let k = 0 , l = 0 ;
let sum1 = 0, sum0 = 0;
for (let i = 1; i < n; i++)
{
if (s[i] == '1' )
sum1++;
else
sum0++;
if (s[i] == s[i-1] && s[i] == '0' )
k++;
else if ( s[i] == s[i-1] && s[i] == '1' )
l++;
}
if (s[0] == '1' )
sum1++;
else
sum0++;
if (Math.abs(sum1 - sum0) > 1)
return -1;
return Math.max(k , l);
}
let S = "10001" ;
let N = S.length;
document.write(minimumReverse(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...