Minimum substring flips required to convert given binary string to another
Last Updated :
11 Jun, 2021
Given two binary strings A and B, the task is to find the minimum number of times a substring starting from the first character of A needs to be flipped, i.e. convert 1s to 0s and 0s to 1s, to convert A to B.
Examples:
Input: A = “0010”, B = “1011”
Output; 3
Explanation:
Step 1: Flip the entire string A. Therefore, A becomes “1101” .
Step 2: Flip the substring {A[0], A[2]}. Therefore, A becomes “0011” .
Step 3: Flip A[0]. Therefore, A becomes “1011” which is equal to B.
Therefore, the minimum number of operations required is 3.
Input: A = “1010101”, B = “0011100”
Output: 5
Explanation:
Step 1: Flip the entiThehrefore, A becomes “0101010″
Step 2: Flip substring {A[0], A[5]}. Therefore, A becomes “1010100″
Step 3: Flip the substring {A[0], A[3]}. Therefore, A becomes “0101100″
Step 4: Flip the substring {A[0], A[2]}. Therefore, A becomes “1011100″
Step 5: Flip A[0]. Therefore, A becomes “0011100” which is equal to B.
Therefore, the minimum number of operations required is 5.
Approach: The idea is to initialize a variable that holds the last index at which character at A is different from the character at B. Then negate A from the 1st index to the last index. Repeat until both strings become equal. Follow the steps below to solve the problem:
- Initialize a variable last_index that holds the last index at which characters are different in A and B.
- Negate string A from the 1st index to last_index and increment the count of steps.
- Repeat the above steps until string A becomes equal to string B.
- Print the count of steps after both the strings are the same after performing the operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimumOperations(string a,
string b)
{
int step = 0;
int last_index;
while (a != b) {
for ( int i = 0;
i < a.length(); i++) {
if (a[i] != b[i]) {
last_index = i;
}
}
for ( int i = 0;
i <= last_index; i++) {
a[i] = (a[i] == '0' ) ? '1' : '0' ;
}
step++;
}
cout << step;
}
int main()
{
string A = "101010" , B = "110011" ;
findMinimumOperations(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMinimumOperations( char [] a,
char [] b)
{
int step = 0 ;
int last_index = 0 ;
while (!Arrays.equals(a, b))
{
for ( int i = 0 ;
i < a.length; i++)
{
if (a[i] != b[i])
{
last_index = i;
}
}
for ( int i = 0 ;
i <= last_index; i++)
{
a[i] = (a[i] == '0' ) ?
'1' : '0' ;
}
step++;
}
System.out.print(step);
}
public static void main(String[] args)
{
String A = "101010" ,
B = "110011" ;
findMinimumOperations(A.toCharArray(),
B.toCharArray());
}
}
|
Python3
def findMinimumOperations(a, b):
step = 0
last_index = 0
while (a ! = b):
a = [i for i in a]
for i in range ( len (a)):
if (a[i] ! = b[i]):
last_index = i
for i in range (last_index + 1 ):
if (a[i] = = '0' ):
a[i] = '1'
else :
a[i] = '0'
a = "".join(a)
step + = 1
print (step)
if __name__ = = '__main__' :
A = "101010"
B = "110011"
findMinimumOperations(A, B)
|
C#
using System;
class GFG{
static void findMinimumOperations( string a,
string b)
{
int step = 0;
int last_index = 0;
while (a.Equals(b) == false )
{
for ( int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
{
last_index = i;
}
}
char [] ch = a.ToCharArray();
for ( int i = 0;
i <= last_index; i++)
{
if (ch[i] == '0' )
{
ch[i] = '1' ;
}
else
{
ch[i] = '0' ;
}
}
a = new string (ch);
step++;
}
Console.WriteLine(step);
}
public static void Main()
{
string A = "101010" ;
string B = "110011" ;
findMinimumOperations(A,B);
}
}
|
Javascript
<script>
function findMinimumOperations(a, b)
{
var step = 0;
var last_index = 0;
while (a !== b)
{
for ( var i = 0; i < a.length; i++)
{
if (a[i] !== b[i])
{
last_index = i;
}
}
var ch = a.split( "" );
for ( var i = 0; i <= last_index; i++)
{
if (ch[i] === "0" )
{
ch[i] = "1" ;
}
else
{
ch[i] = "0" ;
}
}
a = ch.join( "" );
step++;
}
document.write(step);
}
var A = "101010" ;
var B = "110011" ;
findMinimumOperations(A, B);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...