# Check if two binary strings can be made equal by swapping 1s occurring before 0s

• Last Updated : 31 Mar, 2022

Given two binary strings str1 and str2 having same length, the task is to find if it is possible to make the two binary strings str1 and str2 equal by swapping all 1s occurring at indices less than that of 0s index in the binary string str1.

Examples:

Input: str1 = “0110”, str2 = “0011”
Output: Possible
Explanation:
Swapping str1 with str1 the binary string str1 becomes “0101”.
Swapping str1 with str1 the binary string str1 becomes “0011” .
The binary string str1 becomes equal to the binary string str2 therefore, the required output is Possible.

Input: str1 = “101”, str2 = “010”
Output: Not Possible

Approach: The idea is to count the number of 1s and 0s in str1 and str2 and then proceed accordingly. Follow the steps below to solve the problem:

• If the count of 1s and 0s are not equal in str1 and str2, then conversion is not possible.
• Traverse the string.
• Starting from the first character, compare each character one by one. For each different character at i, perform the following steps:
• Check if the current character of the string str1 is ‘0’ and curStr1Ones (stores the current count of 1’s of the string str1) is greater than 0. If found to be true, then swap the character with ‘1’ and decrement the value of curStr1Ones by 1.
• Check if the character of the string str1 is ‘0’ and curStr1Ones is equal to 0. If found to be true, then increment the value of the flag by 1 and break the loop.
• Check if the character of the string str1 is ‘1’ and the character of the string str2 is ‘0’. If found to be true, then swap the character of str1 with ‘0’ and increment the value of curStr1Ones by 1.
• Finally, print “Possible” if the flag is 0, otherwise print “Not Possible”.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to check if it is possible to make``// two binary strings equal by given operations``void` `isBinaryStringsEqual(string str1, string str2)``{` `    ``// Stores count of 1's and 0's``    ``// of the string str1``    ``int` `str1Zeros = 0, str1Ones = 0;` `    ``// Stores count of 1's and 0's``    ``// of the string str2``    ``int` `str2Zeros = 0, str2Ones = 0;` `    ``int` `flag = 0;` `    ``// Stores current count of 1's``    ``// present in the string str1``    ``int` `curStr1Ones = 0;` `    ``// Count the number of 1's and 0's``    ``// present in the strings str1 and str2``    ``for` `(``int` `i = 0; i < str1.length(); i++) {` `        ``if` `(str1[i] == ``'1'``) {``            ``str1Ones++;``        ``}` `        ``else` `if` `(str1[i] == ``'0'``) {``            ``str1Zeros++;``        ``}` `        ``if` `(str2[i] == ``'1'``) {``            ``str2Ones++;``        ``}` `        ``else` `if` `(str2[i] == ``'0'``) {``            ``str2Zeros++;``        ``}``    ``}` `    ``// If the number of 1's and 0's``    ``// are not same of the strings str1``    ``// and str2 then print not possible``    ``if` `(str1Zeros != str2Zeros && str1Ones != str2Ones) {``        ``cout << ``"Not Possible"``;``    ``}` `    ``else` `{` `        ``// Traversing through the``        ``// strings str1 and str2``        ``for` `(``int` `i = 0; i < str1.length(); i++) {` `            ``// If the str1 character not``            ``// equals to str2 character``            ``if` `(str1[i] != str2[i]) {` `                ``// Swaps 0 with 1 of the``                ``// string str1``                ``if` `(str1[i] == ``'0'` `&& curStr1Ones > 0) {``                    ``str1[i] = ``'1'``;``                    ``curStr1Ones--;``                ``}` `                ``// Breaks the loop as the count``                ``// of 1's is zero. Hence, no swaps possible``                ``if` `(str1[i] == ``'0'` `&& curStr1Ones == 0) {` `                    ``flag++;``                    ``break``;``                ``}` `                ``// Swaps 1 with 0 in the string str1``                ``if` `(str1[i] == ``'1'` `&& str2[i] == ``'0'``) {` `                    ``str1[i] = ``'0'``;``                    ``curStr1Ones++;``                ``}``            ``}``        ``}` `        ``if` `(flag == 0) {``            ``cout << ``"Possible"``;``        ``}` `        ``// Print not possible``        ``else` `{``            ``cout << ``"Not Possible"``;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given Strings``    ``string str1 = ``"0110"``;``    ``string str2 = ``"0011"``;` `    ``// Function Call``    ``isBinaryStringsEqual(str1, str2);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``class` `GFG {` `  ``// Function to check if it is possible to make``  ``// two binary strings equal by given operations``  ``static` `void` `isBinaryStringsEqual(String str1,``                                   ``String str2)``  ``{` `    ``// Stores count of 1's and 0's``    ``// of the string str1``    ``int` `str1Zeros = ``0``, str1Ones = ``0``;` `    ``// Stores count of 1's and 0's``    ``// of the string str2``    ``int` `str2Zeros = ``0``, str2Ones = ``0``;``    ``int` `flag = ``0``;` `    ``// Stores current count of 1's``    ``// present in the string str1``    ``int` `curStr1Ones = ``0``;` `    ``// Count the number of 1's and 0's``    ``// present in the strings str1 and str2``    ``for` `(``int` `i = ``0``; i < str1.length(); i++)``    ``{` `      ``if` `(str1.charAt(i) == ``'1'``)``      ``{``        ``str1Ones++;``      ``}` `      ``else` `if` `(str1.charAt(i) == ``'0'``)``      ``{``        ``str1Zeros++;``      ``}` `      ``if` `(str2.charAt(i) == ``'1'``)``      ``{``        ``str2Ones++;``      ``}` `      ``else` `if` `(str2.charAt(i) == ``'0'``)``      ``{``        ``str2Zeros++;``      ``}``    ``}` `    ``// If the number of 1's and 0's``    ``// are not same of the strings str1``    ``// and str2 then print not possible``    ``if` `(str1Zeros != str2Zeros``        ``&& str1Ones != str2Ones)``    ``{``      ``System.out.println(``"Not Possible"``);``    ``}` `    ``else` `{` `      ``// Traversing through the``      ``// strings str1 and str2``      ``for` `(``int` `i = ``0``; i < str1.length(); i++)``      ``{` `        ``// If the str1 character not``        ``// equals to str2 character``        ``if` `(str1.charAt(i) != str2.charAt(i))``        ``{` `          ``// Swaps 0 with 1 of the``          ``// string str1``          ``if` `(str1.charAt(i) == ``'0'``              ``&& curStr1Ones > ``0``)``          ``{``            ``str1 = str1.substring(``0``, i) + ``'1'``              ``+ str1.substring(i + ``1``);``            ``curStr1Ones--;``          ``}` `          ``// Breaks the loop as the count``          ``// of 1's is zero. Hence, no swaps``          ``// possible``          ``if` `(str1.charAt(i) == ``'0'``              ``&& curStr1Ones == ``0``)``          ``{``            ``flag++;``            ``break``;``          ``}` `          ``// Swaps 1 with 0 in the string str1``          ``if` `(str1.charAt(i) == ``'1'``              ``&& str2.charAt(i) == ``'0'``)``          ``{``            ``str1 = str1.substring(``0``, i) + ``'0'``              ``+ str1.substring(i+``1``);``            ``curStr1Ones++;``          ``}``        ``}``      ``}` `      ``if` `(flag == ``0``) {``        ``System.out.println(``"Possible"``);``      ``}` `      ``// Print not possible``      ``else` `{``        ``System.out.println(``"Not Possible"``);``      ``}``    ``}``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{` `    ``// Given Strings``    ``String str1 = ``"0110"``;``    ``String str2 = ``"0011"``;` `    ``// Function Call``    ``isBinaryStringsEqual(str1, str2);``  ``}``}` `// This code is contributed by dharanendralv23`

## Python3

 `# Python program for the above approach` `# Function to check if it is possible to make``# two binary strings equal by given operations``def` `isBinaryStringsEqual(list1, list2) :``    ` `    ``str1 ``=` `list``(list1)``    ``str2 ``=` `list``(list2)` `    ``# Stores count of 1's and 0's``    ``# of the string str1``    ``str1Zeros ``=` `0``    ``str1Ones ``=` `0` `    ``# Stores count of 1's and 0's``    ``# of the string str2``    ``str2Zeros ``=` `0``    ``str2Ones ``=` `0``    ``flag ``=` `0` `    ``# Stores current count of 1's``    ``# present in the string str1``    ``curStr1Ones ``=` `0` `    ``# Count the number of 1's and 0's``    ``# present in the strings str1 and str2``    ``for` `i ``in` `range``(``len``(str1)):``        ``if` `(str1[i] ``=``=` `'1'``) :``            ``str1Ones ``+``=` `1``        ``elif` `(str1[i] ``=``=` `'0'``) :``            ``str1Zeros ``+``=` `1``        ``if` `(str2[i] ``=``=` `'1'``) :``            ``str2Ones ``+``=` `1``        ``elif` `(str2[i] ``=``=` `'0'``) :``            ``str2Zeros ``+``=` `1``       ` `    ``# If the number of 1's and 0's``    ``# are not same of the strings str1``    ``# and str2 then print not possible``    ``if` `(str1Zeros !``=` `str2Zeros ``and` `str1Ones !``=` `str2Ones) :``        ``print``(``"Not Possible"``)``    ``else` `:` `        ``# Traversing through the``        ``# strings str1 and str2``        ``for` `i ``in` `range``(``len``(str1)):` `            ``# If the str1 character not``            ``# equals to str2 character``            ``if` `(str1[i] !``=` `str2[i]) :` `                ``# Swaps 0 with 1 of the``                ``# string str1``                ``if` `(str1[i] ``=``=` `'0'` `and` `curStr1Ones > ``0``) :              ``                    ``str1[i] ``=` `'1'``                    ``curStr1Ones ``-``=` `1``                ` `                ``# Breaks the loop as the count``                ``# of 1's is zero. Hence, no swaps possible``                ``if` `(str1[i] ``=``=` `'0'` `and` `curStr1Ones ``=``=` `0``) :``                    ``flag ``+``=` `1``                    ``break``                ` `                ``# Swaps 1 with 0 in the string str1``                ``if` `(str1[i] ``=``=` `'1'` `and` `str2[i] ``=``=` `'0'``) :``                    ``str1[i] ``=` `'0'``                    ``curStr1Ones ``+``=` `1``                ` `        ``if` `(flag ``=``=` `0``) :``            ``print``(``"Possible"``)``        ` `        ``# Print not possible``        ``else` `:``           ``print``(``"Not Possible"``)` `# Driver Code` `# Given Strings``str1 ``=` `"0110"``str2 ``=` `"0011"` `# Function Call``isBinaryStringsEqual(str1, str2)` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Text;``class` `GFG``{` `  ``// Function to check if it is possible to make``  ``// two binary strings equal by given operations``  ``static` `void` `isBinaryStringsEqual(``string` `str1,``                                   ``string` `str2)``  ``{` `    ``// Stores count of 1's and 0's``    ``// of the string str1``    ``int` `str1Zeros = 0, str1Ones = 0;` `    ``// Stores count of 1's and 0's``    ``// of the string str2``    ``int` `str2Zeros = 0, str2Ones = 0;``    ``int` `flag = 0;` `    ``// Stores current count of 1's``    ``// present in the string str1``    ``int` `curStr1Ones = 0;` `    ``// Count the number of 1's and 0's``    ``// present in the strings str1 and str2``    ``for` `(``int` `i = 0; i < str1.Length; i++)``    ``{``      ``if` `(str1[i] == ``'1'``)``      ``{``        ``str1Ones++;``      ``}` `      ``else` `if` `(str1[i] == ``'0'``)``      ``{``        ``str1Zeros++;``      ``}` `      ``if` `(str2[i] == ``'1'``)``      ``{``        ``str2Ones++;``      ``}` `      ``else` `if` `(str2[i] == ``'0'``)``      ``{``        ``str2Zeros++;``      ``}``    ``}` `    ``// If the number of 1's and 0's``    ``// are not same of the strings str1``    ``// and str2 then print not possible``    ``if` `(str1Zeros != str2Zeros``        ``&& str1Ones != str2Ones)``    ``{``      ``Console.WriteLine(``"Not Possible"``);``    ``}` `    ``else``    ``{` `      ``// Traversing through the``      ``// strings str1 and str2``      ``for` `(``int` `i = 0; i < str1.Length; i++)``      ``{` `        ``// If the str1 character not``        ``// equals to str2 character``        ``if` `(str1[i] != str2[i])``        ``{` `          ``// Swaps 0 with 1 of the``          ``// string str1``          ``if` `(str1[i] == ``'0'` `&& curStr1Ones > 0)``          ``{``            ``StringBuilder sb``              ``= ``new` `StringBuilder(str1);``            ``sb[i] = ``'1'``;``            ``str1 = sb.ToString();``            ``curStr1Ones--;``          ``}` `          ``// Breaks the loop as the count``          ``// of 1's is zero. Hence, no swaps``          ``// possible``          ``if` `(str1[i] == ``'0'``              ``&& curStr1Ones == 0)``          ``{``            ``flag++;``            ``break``;``          ``}` `          ``// Swaps 1 with 0 in the string str1``          ``if` `(str1[i] == ``'1'` `&& str2[i] == ``'0'``)``          ``{``            ``StringBuilder sb``              ``= ``new` `StringBuilder(str1);``            ``sb[i] = ``'0'``;``            ``str1 = sb.ToString();``            ``curStr1Ones++;``          ``}``        ``}``      ``}` `      ``if` `(flag == 0)``      ``{``        ``Console.WriteLine(``"Possible"``);``      ``}` `      ``// Print not possible``      ``else``      ``{``        ``Console.WriteLine(``"Not Possible"``);``      ``}``    ``}``  ``}` `  ``// Driver Code``  ``static` `public` `void` `Main()``  ``{` `    ``// Given Strings``    ``string` `str1 = ``"0110"``;``    ``string` `str2 = ``"0011"``;` `    ``// Function Call``    ``isBinaryStringsEqual(str1, str2);``  ``}``}` `// This code is contributed by dharanendralv23`

## Javascript

 ``

Output

`Possible`

Time Complexity: O(|str1|)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up