Minimum steps to convert one binary string to other only using negation
Given two binary strings A and B, the task is to convert A to B by choosing any sub-string of A and negating it (replace each 0 with 1 and each 1 with 0). Print the minimum number of operations required.
Examples:
Input: A = “101010”, B = “110011”
Output: 2
Choose the sub-string of length 2 from index 1 to 2 and negate it then A becomes “110010” and then take the last character and negate it.
The final string becomes “110011”Input: A = “1010101”, B = “0011100”
Output: 3
Approach:
Create a blank array and mark the indices which need to be negated. Then the answer will be number of blocks of consecutive 1’s in the array as a single block can be negated in a single operation.
For example, A = “101010” and B = “110011”
The newly created array will be {0, 1, 1, 0, 0, 1} so the answer will be 2,
A after first operation will be “110010”
After second operation “110011”
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <iostream> using namespace std; // Function to find the minimum steps // to convert string a to string b void convert( int n, string a, string b) { // array to mark the positions // needed to be negated int l[n]; int i; for (i = 0; i < n; i++) l[i] = 0; for (i = 0; i < n; i++) { // If two character are not same // then they need to be negated if (a[i] != b[i]) l[i] = 1; } // To count the blocks of 1 int cc = 0; // To count the number of 1's in // each block of 1's int vl = 0; for (i = 0; i < n; i++) { if (l[i] == 0) { if (vl != 0) cc += 1; vl = 0; } else vl += 1; } // For the last block of 1's if (vl != 0) cc += 1; cout << cc << endl; } // Driver code int main() { string a = "101010" ; string b = "110011" ; int n = a.length(); convert(n, a, b); return 0; } // This code is contributed by ANKITRAI1 |
Java
// Java implementation of the above approach import java.util.*; class solution { // Function to find the minimum steps // to convert string a to string b static void convert( int n, String a, String b) { // array to mark the positions // needed to be negated int [] l = new int [n]; int i; for (i = 0 ; i < n; i++) l[i] = 0 ; for (i = 0 ; i < n; i++) { // If two character are not same // then they need to be negated if (a.charAt(i) != b.charAt(i)) l[i] = 1 ; } // To count the blocks of 1 int cc = 0 ; // To count the number of 1's in // each block of 1's int vl = 0 ; for (i = 0 ; i < n; i++) { if (l[i] == 0 ) { if (vl != 0 ) cc += 1 ; vl = 0 ; } else vl += 1 ; } // For the last block of 1's if (vl != 0 ) cc += 1 ; System.out.println(cc); } // Driver code public static void main(String args[]) { String a = "101010" ; String b = "110011" ; int n = a.length(); convert(n, a, b); } } |
Python3
# Python3 implementation of the above approach # Function to find the minimum steps # to convert string a to string b def convert(n, a, b): # List to mark the positions needed to # be negated l = [ 0 ] * n for i in range (n): # If two character are not same # then they need to be negated if (a[i] ! = b[i]): l[i] = 1 # To count the blocks of 1 cc = 0 # To count the number of 1's in each # block of 1's vl = 0 for i in range (n): if (l[i] = = 0 ): if (vl ! = 0 ): cc + = 1 vl = 0 else : vl + = 1 # For the last block of 1's if (vl ! = 0 ): cc + = 1 print (cc) # Driver code a = "101010" b = "110011" n = len (a) convert(n, a, b) |
C#
// C# implementation of the above approach using System; class GFG { // Function to find the minimum steps // to convert string a to string b static void convert( int n, String a, String b) { // array to mark the positions // needed to be negated int [] l = new int [n]; int i; for (i = 0; i < n; i++) l[i] = 0; for (i = 0; i < n; i++) { // If two character are not same // then they need to be negated if (a[i] != b[i]) l[i] = 1; } // To count the blocks of 1 int cc = 0; // To count the number of 1's in // each block of 1's int vl = 0; for (i = 0; i < n; i++) { if (l[i] == 0) { if (vl != 0) cc += 1; vl = 0; } else vl += 1; } // For the last block of 1's if (vl != 0) cc += 1; Console.WriteLine(cc); } // Driver code static public void Main() { String a = "101010" ; String b = "110011" ; int n = a.Length; convert(n, a, b); } } // This code is contributed by jit_t. |
PHP
<?php // PHP implementation of the above approach // Function to find the minimum steps // to convert string a to string b function convert( $n , $a , $b ) { // array to mark the positions // needed to be negated $l = array_fill (0, $n , NULL); for ( $i = 0; $i < $n ; $i ++) $l [ $i ] = 0 ; for ( $i = 0; $i < $n ; $i ++) { // If two character are not same // then they need to be negated if ( $a [ $i ] != $b [ $i ]) $l [ $i ] = 1 ; } // To count the blocks of 1 $cc = 0; // To count the number of 1's in // each block of 1's $vl = 0 ; for ( $i = 0; $i < $n ; $i ++) { if ( $l [ $i ] == 0) { if ( $vl != 0) $cc += 1 ; $vl = 0 ; } else $vl += 1 ; } // For the last block of 1's if ( $vl != 0) $cc += 1 ; echo $cc . "\n" ; } // Driver code $a = "101010" ; $b = "110011" ; $n = strlen ( $a ); convert( $n , $a , $b ) ; // This code is contributed // by ChitraNayal ?> |
Javascript
<script> // Javascript implementation of // the above approach // Function to find the minimum steps // to convert string a to string b function convert(n, a, b) { // array to mark the positions // needed to be negated let l = new Array(n); let i; for (i = 0; i < n; i++) l[i] = 0; for (i = 0; i < n; i++) { // If two character are not same // then they need to be negated if (a[i] != b[i]) l[i] = 1; } // To count the blocks of 1 let cc = 0; // To count the number of 1's in // each block of 1's let vl = 0; for (i = 0; i < n; i++) { if (l[i] == 0) { if (vl != 0) cc += 1; vl = 0; } else vl += 1; } // For the last block of 1's if (vl != 0) cc += 1; document.write(cc + "</br>" ); } let a = "101010" ; let b = "110011" ; let n = a.length; convert(n, a, b); </script> |
2
Time complexity: O(n)
Auxiliary space: O(n)
Please Login to comment...