Convert given Binary String to another in minimum operations by flipping all bits except any 1
Given two binary strings s1 and s2, the task is to count the minimum operations to convert string s1 to s2. In one operation a set bit can be chosen and all other bits except that is flipped. If it is not possible to convert s1-> s2 print -1.
Examples:
Input: s1 = “100010111” s2 = “101101100”
Output: 3
Explanation:
In the first operation hold 1 at 6 th index and convert remaining to logical NOT: 100010111→011101100 .
In the second operation hold 1 at 1st index and convert remaining to logical NOT: 011101100→110010011.
In the third operation hold 1 at 0th index and convert remaining to logical NOT: 110010011→101101100.
So 3 operations.Input: s1=”11111″ s2 = “00000”
Output: -1
Approach: It can be seen choosing the same index of 1 after two operations string remains the same as the original. So different index should be chosen for each operation on doing so “10” in string s1 can be converted to a string “01” in string s2 with two operations. So the answer is dependent on minimizing the number of “10” and “01” in strings s1 and s2. If at any index “0(s1) – 1(s2) “ && “1(s1) – 0(s2)” are equal in number then there is an answer else -1.
“01” (on choosing 1 at index1) -> “11”(on choosing 1 at index2) -> “10”
Using this conclusion:
It can be even possible that minimum “0(s1) – 1(s2) ” && “1(s1) – 0(s2)” pairs can be obtained by doing 1 operation initially. In the cases where 1 (s1)-> 1(s2) or 1(s1) -> 0(s2).
Follow these steps to solve this problem:
- Initialize the variable res = INT_MAX
- Initialize the variable ops1= -1 to store the operations required to convert string s1 to s2 without any modification.
- Now check if it is possible to minimize operations by doing 1 initial modification in case of (1(s1)-> 1(s2)).
- Store the operations in ops2 variable and store the minimum in the res by doing min(res,ops2).
- Now check if it is possible to minimize operations by doing 1 initial modification in case of (1(s1)-> 0(s2)).
- Store the operations in ops3 variable and store the minimum in the res by doing min(res,ops3).
- If res is equal to INT_MAX it means it is not possible to convert string s1 -> s2 so print -1.
- Else print the res.
Below is the implementation of the above approach:
C++14
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to count the "0(s1)-1(s2)" // && "1(s1)- 0(s2)" pairsint count_operations(string s1, string s2){ int n = s1.size(); // Initializing to 0 initially int cnt10 = 0, cnt01 = 0; for (int i = 0; i < n; i++) { if (s1[i] != s2[i]) { if (s1[i] == '0') cnt01++; else cnt10++; } } // Equal 0(s1)-1(s2) and 1(s1)- 0(s2) pairs // To convert 1 pair 2 operations are required // so 2 * cnt01 if (cnt01 == cnt10) return 2 * cnt01; return -1;} // Function to do one operation of// modifying the string s1bool modify_string(string& s1, string& s2, char c){ int n = s1.size(); int idx = -1; // Find the index of occurence of // 1(s1)- c(s2) in s1 for (int i = 0; i < n; i++) { if (s1[i] == '1' && s2[i] == c) { // Break if found idx = i; break; } } if (idx == -1) return 0; // Flip the remaining except that index for (int i = 0; i < n; i++) { if (i == idx) continue; if (s1[i] == '1') s1[i] = '0'; else s1[i] = '1'; } return 1;} // Function to find the minimum operations // to convert the string s1 to string s2int find_min_operations(string s1, string s2){ int res = INT_MAX; // Case -1 Initial strings itself int ops1 = count_operations(s1, s2); if (ops1 != -1) res = min(res, ops1); string a = s1, b = s2; // Case -2 Doing 1 modification initailly // for 1(s1)-1(s2) if (modify_string(a, b, '1')) { int ops2 = count_operations(a, b); // Take minimum if (ops2 != -1) res = min(res, 1 + ops2); } // Case-3 doing 1 modification initailly // for 1(s1) - 0(s2) a = s1, b = s2; if (modify_string(a, b, '0')) { int ops3 = count_operations(a, b); // Take minimum if (ops3 != -1) res = min(res, 1 + ops3); } if (res == INT_MAX) return -1; else return res;} // Driver codeint main(){ string s1 = "100010111"; string s2 = "101101100"; // Function call cout << find_min_operations(s1, s2) << endl; return 0;} |
3
Time Complexity: O(N) where N is the length of the string.
Space Complexity: O(N)


 (1).png)