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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function that finds the minimum
// number of operations required such
// that string A and B are the same
void findMinimumOperations(string a,
string b)
{
// Stores the count of steps
int step = 0;
// Stores the last index whose
// bits are not same
int last_index;
// Iterate until both string
// are unequal
while (a != b) {
// Check till end of string to
// find rightmost unequals bit
for (int i = 0;
i < a.length(); i++) {
// Update the last index
if (a[i] != b[i]) {
last_index = i;
}
}
// Flipping characters up
// to the last index
for (int i = 0;
i <= last_index; i++) {
// Flip the bit
a[i] = (a[i] == '0') ? '1' : '0';
}
// Increasing steps by one
step++;
}
// Print the count of steps
cout << step;
}
// Driver Code
int main()
{
// Given strings A and B
string A = "101010", B = "110011";
// Function Call
findMinimumOperations(A, B);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
// Function that finds the minimum
// number of operations required such
// that String A and B are the same
static void findMinimumOperations(char[] a,
char[] b)
{
// Stores the count of steps
int step = 0;
// Stores the last index whose
// bits are not same
int last_index = 0;
// Iterate until both String
// are unequal
while (!Arrays.equals(a, b))
{
// Check till end of String to
// find rightmost unequals bit
for (int i = 0;
i < a.length; i++)
{
// Update the last index
if (a[i] != b[i])
{
last_index = i;
}
}
// Flipping characters up
// to the last index
for (int i = 0;
i <= last_index; i++)
{
// Flip the bit
a[i] = (a[i] == '0') ?
'1' : '0';
}
// Increasing steps by one
step++;
}
// Print the count of steps
System.out.print(step);
}
// Driver Code
public static void main(String[] args)
{
// Given Strings A and B
String A = "101010",
B = "110011";
// Function Call
findMinimumOperations(A.toCharArray(),
B.toCharArray());
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function that finds the minimum
# number of operations required such
# that string A and B are the same
def findMinimumOperations(a, b):
# Stores the count of steps
step = 0
# Stores the last index whose
# bits are not same
last_index = 0
# Iterate until both string
# are unequal
while (a != b):
a = [i for i in a]
# Check till end of string to
# find rightmost unequals bit
for i in range(len(a)):
# Update the last index
if (a[i] != b[i]):
last_index = i
# Flipping characters up
# to the last index
for i in range(last_index + 1):
# Flip the bit
if (a[i] == '0'):
a[i] = '1'
else:
a[i] = '0'
a = "".join(a)
# Increasing steps by one
step += 1
# Print the count of steps
print(step)
# Driver Code
if __name__ == '__main__':
# Given strings A and B
A = "101010"
B = "110011"
# Function Call
findMinimumOperations(A, B)
# This code is contributed by mohit kumar 29
C#
// C# program for the
// above approach
using System;
class GFG{
// Function that finds the minimum
// number of operations required such
// that string A and B are the same
static void findMinimumOperations(string a,
string b)
{
// Stores the count of steps
int step = 0;
// Stores the last index whose
// bits are not same
int last_index = 0;
// Iterate until both string
// are unequal
while (a.Equals(b) == false)
{
// Check till end of string to
// find rightmost unequals bit
for(int i = 0; i < a.Length; i++)
{
// Update the last index
if (a[i] != b[i])
{
last_index = i;
}
}
// Flipping characters up
// to the last index
char[] ch = a.ToCharArray();
for(int i = 0;
i <= last_index; i++)
{
// Flip the bit
if (ch[i] == '0')
{
ch[i] = '1';
}
else
{
ch[i] = '0';
}
}
a = new string(ch);
// Increasing steps by one
step++;
}
// Print the count of steps
Console.WriteLine(step);
}
// Driver Code
public static void Main()
{
// Given strings A and B
string A = "101010";
string B = "110011";
// Function Call
findMinimumOperations(A,B);
}
}
// This code is contributed by SURENDRA_GANGWAR
JavaScript
<script>
// JavaScript program for the
// above approach
// Function that finds the minimum
// number of operations required such
// that string A and B are the same
function findMinimumOperations(a, b)
{
// Stores the count of steps
var step = 0;
// Stores the last index whose
// bits are not same
var last_index = 0;
// Iterate until both string
// are unequal
while (a !== b)
{
// Check till end of string to
// find rightmost unequals bit
for (var i = 0; i < a.length; i++)
{
// Update the last index
if (a[i] !== b[i])
{
last_index = i;
}
}
// Flipping characters up
// to the last index
var ch = a.split("");
for(var i = 0; i <= last_index; i++)
{
// Flip the bit
if (ch[i] === "0")
{
ch[i] = "1";
}
else
{
ch[i] = "0";
}
}
a = ch.join("");
// Increasing steps by one
step++;
}
// Print the count of steps
document.write(step);
}
// Driver Code
// Given strings A and B
var A = "101010";
var B = "110011";
// Function Call
findMinimumOperations(A, B);
// This code is contributed by rdtank
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Minimum substring flips required to convert a Binary String to another Given two binary strings S1 and S2 of size N and M respectively, the task is to find the minimum number of reversal of substrings of equal characters required to convert the string S1 to S2. If it is not possible to convert the string S1 to S2, then print "-1". Examples: Input: S1 = "100001", S2 = "
6 min read
Minimum swaps required to convert one binary string to another Given two binary string M and N of equal length, the task is to find a minimum number of operations (swaps) required to convert string N to M. Examples: Input: str1 = "1101", str2 = "1110" Output: 1 Swap last and second last element in the binary string, so that it become 1101 Input: str1 = "1110000
5 min read
Minimum prefixes required to be flipped to convert a Binary String to another Given two binary strings A and B of length N, the task is to convert the string from A to string B by repeatedly flipping all the bits of a prefix of A, i.e. convert all the 0s in the prefix to 1s and vice-versa and print the minimum number of prefix flips required and the length of respective prefi
9 min read
Minimum flips required in a binary string such that all K-size substring contains 1 Given a binary string str of size N and a positive integer K, the task is to find the minimum number of flips required to make all substring of size K contain at least one '1'.Examples: Input: str = "0001", K = 2 Output: 1 Explanation: Flipping the bit at index 1 modifies str to "0101". All substrin
11 min read
Minimum number of given operations required to convert a string to another string Given two strings S and T of equal length. Both strings contain only the characters '0' and '1'. The task is to find the minimum number of operations to convert string S to T. There are 2 types of operations allowed on string S: Swap any two characters of the string.Replace a '0' with a '1' or vice
14 min read