Open In App

Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1s

Last Updated : 09 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two binary strings A and B of length N, the task is to check if the string A can be converted to B by reversing substrings of A which contains even number of 1s.

Examples:

Input: A = “10011”, B = “11100”
Output: Yes
Explanation: Reverse substring A[2, 5], 10011 → 11100.
After completing the above steps, strings A and B are the same.

Input: A = “10011” B = “00111”
Output: No

 

Approach: The idea is based on the following observations:

  • If the string A can be transformed into string B, then the converse also holds true, since the operations to convert A to B can be reversed to convert B to A.
  • A can only be made equal to B when:
    • Length(A) = Length(B) and the number of 1s in A and B are the same, and
    • cntA= cntB where cntS is the number of Position i where 1 ≤ i ≤ length(S) and (∑ij=1 (Sj))mod 2 = 1.

Follow the steps below to solve the problem:

  1. Traverse the string A and B and store the frequency of 1 in variables, say count1A and count1B respectively.
  2. Initialize a variable, say temp, to store the temporary count of 1s.
  3. Traverse the string A using variable i and perform the following steps: 
    • If the current character is 1, then increment temp by 1.
    • Otherwise, if the value of temp is odd, then increment variable odd1A by 1. Otherwise, increment variable even1A by 1.
  4. Repeat the above steps 2 to 3 for string B also.
  5. After completing the above steps, if the value of count1A and count1B are the same, the value of odd1A and odd1B are the same, and the value of even1A and even1B, is the same then print “Yes” else print “No”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if string A can be
// transformed to string B by reversing
// substrings of A having even number of 1s
void canTransformStrings(string A, string B)
{
    // Store the size of string A
    int n1 = A.size();
 
    // Store the size of string B
    int n2 = B.size();
 
    // Store the count of 1s in A and B
    int count1A = 0, count1B = 0;
 
    // Stores cntA for string A
    // and cntB for string B
    int odd1A = 0, odd1B = 0;
    int even1A = 0, even1B = 0;
 
    // Traverse the string A
    for (int i = 0; i < n1; i++) {
 
        // If current character is 1
        if (A[i] == '1')
 
            // Increment 1s count
            count1A++;
 
        // Otherwise, update odd1A or
        // even1A depending whether
        // count1A is odd or even
        else {
            if (count1A & 1)
                odd1A++;
            else
                even1A++;
        }
    }
 
    // Traverse the string B
    for (int i = 0; i < n2; i++) {
 
        // If current character is 1
        if (B[i] == '1')
 
            // Increment 1s count
            count1B++;
 
        // Otherwise, update odd1B or
        // even1B depending whether
        // count1B is odd or even
        else {
            if (count1B & 1)
                odd1B++;
            else
                even1B++;
        }
    }
 
    // If the condition is satisfied
    if (count1A == count1B
        && odd1A == odd1B
        && even1A == even1B) {
 
        // If true, print Yes
        cout << "Yes";
    }
 
    // Otherwise, print No
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    string A = "10011", B = "11100";
 
    // Function Call
    canTransformStrings(A, B);
 
    return 0;
}


Java




// Java program for the above approach
class GFG{
     
// Function to check if string A can be
// transformed to string B by reversing
// substrings of A having even number of 1s
public static void canTransformStrings(String A,
                                       String B)
{
     
    // Store the size of string A
    int n1 = A.length();
   
    // Store the size of string B
    int n2 = B.length();
   
    // Store the count of 1s in A and B
    int count1A = 0, count1B = 0;
   
    // Stores cntA for string A
    // and cntB for string B
    int odd1A = 0, odd1B = 0;
    int even1A = 0, even1B = 0;
     
    // Traverse the string A
    for(int i = 0; i < n1; i++)
    {
         
        // If current character is 1
        if (A.charAt(i) == '1')
         
            // Increment 1s count
            count1A++;
   
        // Otherwise, update odd1A or
        // even1A depending whether
        // count1A is odd or even
        else
        {
            if ((count1A & 1) == 1)
                odd1A++;
            else
                even1A++;
        }
    }
   
    // Traverse the string B
    for(int i = 0; i < n2; i++)
    {
         
        // If current character is 1
        if (B.charAt(i) == '1')
         
            // Increment 1s count
            count1B++;
   
        // Otherwise, update odd1B or
        // even1B depending whether
        // count1B is odd or even
        else
        {
            if ((count1B & 1) == 1)
                odd1B++;
            else
                even1B++;
        }
    }
   
    // If the condition is satisfied
    if (count1A == count1B &&
          odd1A == odd1B &&
         even1A == even1B)
    {
         
        // If true, print Yes
        System.out.print("Yes");
    }
   
    // Otherwise, print No
    else
        System.out.print("No");
}
 
// Driver Code
public static void main(String[] args)
{
    String A = "10011", B = "11100";
     
    // Function Call
    canTransformStrings(A, B);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3




# Python program for the above approach
 
# Function to check if string A can be
# transformed to string B by reversing
# substrings of A having even number of 1s
def canTransformStrings(A, B):
   
    # Store the size of string A
    n1 = len(A);
 
    # Store the size of string B
    n2 = len(B);
 
    # Store the count of 1s in A and B
    count1A = 0;
    count1B = 0;
 
    # Stores cntA for string A
    # and cntB for string B
    odd1A = 0; odd1B = 0;
    even1A = 0; even1B = 0;
 
    # Traverse the string A
    for i in range(n1):
 
        # If current character is 1
        if (A[i] == '1'):
 
            # Increment 1s count
            count1A += 1;
 
        # Otherwise, update odd1A or
        # even1A depending whether
        # count1A is odd or even
        else:
            if ((count1A & 1) == 1):
                odd1A += 1;
            else:
                even1A += 1;
 
    # Traverse the string B
    for i in range(n2):
 
        # If current character is 1
        if (B[i] == '1'):
 
            # Increment 1s count
            count1B += 1;
 
        # Otherwise, update odd1B or
        # even1B depending whether
        # count1B is odd or even
        else:
            if ((count1B & 1) == 1):
                odd1B += 1;
            else:
                even1B += 1;
 
    # If the condition is satisfied
    if (count1A == count1B and odd1A == odd1B and even1A == even1B):
 
        # If True, print Yes
        print("Yes");
 
    # Otherwise, print No
    else:
        print("No");
 
# Driver Code
if __name__ == '__main__':
    A = "10011";
    B = "11100";
 
    # Function Call
    canTransformStrings(A, B);
 
# This code is contributed by Princi Singh


C#




// C# program for the above approach
using System;
using System.Collections;
class GFG {
     
    // Function to check if string A can be
    // transformed to string B by reversing
    // substrings of A having even number of 1s
    static void canTransformStrings(string A, string B)
    {
          
        // Store the size of string A
        int n1 = A.Length;
        
        // Store the size of string B
        int n2 = B.Length;
        
        // Store the count of 1s in A and B
        int count1A = 0, count1B = 0;
        
        // Stores cntA for string A
        // and cntB for string B
        int odd1A = 0, odd1B = 0;
        int even1A = 0, even1B = 0;
          
        // Traverse the string A
        for(int i = 0; i < n1; i++)
        {
              
            // If current character is 1
            if (A[i] == '1')
              
                // Increment 1s count
                count1A++;
        
            // Otherwise, update odd1A or
            // even1A depending whether
            // count1A is odd or even
            else
            {
                if ((count1A & 1) == 1)
                    odd1A++;
                else
                    even1A++;
            }
        }
        
        // Traverse the string B
        for(int i = 0; i < n2; i++)
        {
              
            // If current character is 1
            if (B[i] == '1')
              
                // Increment 1s count
                count1B++;
        
            // Otherwise, update odd1B or
            // even1B depending whether
            // count1B is odd or even
            else
            {
                if ((count1B & 1) == 1)
                    odd1B++;
                else
                    even1B++;
            }
        }
        
        // If the condition is satisfied
        if (count1A == count1B &&
              odd1A == odd1B &&
             even1A == even1B)
        {
              
            // If true, print Yes
            Console.Write("Yes");
        }
        
        // Otherwise, print No
        else
            Console.Write("No");
    
 
  static void Main()
  {
    string A = "10011", B = "11100";
      
    // Function Call
    canTransformStrings(A, B);
  }
}
 
// This code is contributed by divyesh072019


Javascript




<script>
 
// JavaScript program for above approach
 
    // Function to check if string A can be
    // transformed to string B by reversing
    // substrings of A having even number of 1s
    function canTransformStrings(A, B)
    {
           
        // Store the size of string A
        let n1 = A.length;
         
        // Store the size of string B
        let n2 = B.length;
         
        // Store the count of 1s in A and B
        let count1A = 0, count1B = 0;
         
        // Stores cntA for string A
        // and cntB for string B
        let odd1A = 0, odd1B = 0;
        let even1A = 0, even1B = 0;
           
        // Traverse the string A
        for(let i = 0; i < n1; i++)
        {
               
            // If current character is 1
            if (A[i] == '1')
               
                // Increment 1s count
                count1A++;
         
            // Otherwise, update odd1A or
            // even1A depending whether
            // count1A is odd or even
            else
            {
                if ((count1A & 1) == 1)
                    odd1A++;
                else
                    even1A++;
            }
        }
         
        // Traverse the string B
        for(let i = 0; i < n2; i++)
        {
               
            // If current character is 1
            if (B[i] == '1')
               
                // Increment 1s count
                count1B++;
         
            // Otherwise, update odd1B or
            // even1B depending whether
            // count1B is odd or even
            else
            {
                if ((count1B & 1) == 1)
                    odd1B++;
                else
                    even1B++;
            }
        }
         
        // If the condition is satisfied
        if (count1A == count1B &&
              odd1A == odd1B &&
             even1A == even1B)
        {
               
            // If true, print Yes
            document.write("Yes");
        }
         
        // Otherwise, print No
        else
            document.write("No");
    }
 
// Driver Code
 
    let A = "10011", B = "11100";
       
    // Function Call
    canTransformStrings(A, B);
 
</script>


Output: 

Yes

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads