Open in App
Not now

Minimum steps to convert one binary string to other only using negation

• Difficulty Level : Basic
• Last Updated : 30 Sep, 2022

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:
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:

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 ` `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

 ``

Javascript

 ``

Output

`2`

Time complexity: O(n)
Auxiliary space: O(n)

My Personal Notes arrow_drop_up
Related Articles