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** 0**s 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 righmost 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;

}

Output:

4

Time Complexity:_ O(N2)_

Auxiliary Space:_ O(1)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#greedy #searching #strings #binary-string #substring

Minimum substring flips required to convert given binary string to another
14.30 GEEK