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:
- Traverse the string A and B and store the frequency of 1 in variables, say count1A and count1B respectively.
- Initialize a variable, say temp, to store the temporary count of 1s.
- 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.
- Repeat the above steps 2 to 3 for string B also.
- 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++
#include <bits/stdc++.h>
using namespace std;
void canTransformStrings(string A, string B)
{
int n1 = A.size();
int n2 = B.size();
int count1A = 0, count1B = 0;
int odd1A = 0, odd1B = 0;
int even1A = 0, even1B = 0;
for ( int i = 0; i < n1; i++) {
if (A[i] == '1' )
count1A++;
else {
if (count1A & 1)
odd1A++;
else
even1A++;
}
}
for ( int i = 0; i < n2; i++) {
if (B[i] == '1' )
count1B++;
else {
if (count1B & 1)
odd1B++;
else
even1B++;
}
}
if (count1A == count1B
&& odd1A == odd1B
&& even1A == even1B) {
cout << "Yes" ;
}
else
cout << "No" ;
}
int main()
{
string A = "10011" , B = "11100" ;
canTransformStrings(A, B);
return 0;
}
|
Java
class GFG{
public static void canTransformStrings(String A,
String B)
{
int n1 = A.length();
int n2 = B.length();
int count1A = 0 , count1B = 0 ;
int odd1A = 0 , odd1B = 0 ;
int even1A = 0 , even1B = 0 ;
for ( int i = 0 ; i < n1; i++)
{
if (A.charAt(i) == '1' )
count1A++;
else
{
if ((count1A & 1 ) == 1 )
odd1A++;
else
even1A++;
}
}
for ( int i = 0 ; i < n2; i++)
{
if (B.charAt(i) == '1' )
count1B++;
else
{
if ((count1B & 1 ) == 1 )
odd1B++;
else
even1B++;
}
}
if (count1A == count1B &&
odd1A == odd1B &&
even1A == even1B)
{
System.out.print( "Yes" );
}
else
System.out.print( "No" );
}
public static void main(String[] args)
{
String A = "10011" , B = "11100" ;
canTransformStrings(A, B);
}
}
|
Python3
def canTransformStrings(A, B):
n1 = len (A);
n2 = len (B);
count1A = 0 ;
count1B = 0 ;
odd1A = 0 ; odd1B = 0 ;
even1A = 0 ; even1B = 0 ;
for i in range (n1):
if (A[i] = = '1' ):
count1A + = 1 ;
else :
if ((count1A & 1 ) = = 1 ):
odd1A + = 1 ;
else :
even1A + = 1 ;
for i in range (n2):
if (B[i] = = '1' ):
count1B + = 1 ;
else :
if ((count1B & 1 ) = = 1 ):
odd1B + = 1 ;
else :
even1B + = 1 ;
if (count1A = = count1B and odd1A = = odd1B and even1A = = even1B):
print ( "Yes" );
else :
print ( "No" );
if __name__ = = '__main__' :
A = "10011" ;
B = "11100" ;
canTransformStrings(A, B);
|
C#
using System;
using System.Collections;
class GFG {
static void canTransformStrings( string A, string B)
{
int n1 = A.Length;
int n2 = B.Length;
int count1A = 0, count1B = 0;
int odd1A = 0, odd1B = 0;
int even1A = 0, even1B = 0;
for ( int i = 0; i < n1; i++)
{
if (A[i] == '1' )
count1A++;
else
{
if ((count1A & 1) == 1)
odd1A++;
else
even1A++;
}
}
for ( int i = 0; i < n2; i++)
{
if (B[i] == '1' )
count1B++;
else
{
if ((count1B & 1) == 1)
odd1B++;
else
even1B++;
}
}
if (count1A == count1B &&
odd1A == odd1B &&
even1A == even1B)
{
Console.Write( "Yes" );
}
else
Console.Write( "No" );
}
static void Main()
{
string A = "10011" , B = "11100" ;
canTransformStrings(A, B);
}
}
|
Javascript
<script>
function canTransformStrings(A, B)
{
let n1 = A.length;
let n2 = B.length;
let count1A = 0, count1B = 0;
let odd1A = 0, odd1B = 0;
let even1A = 0, even1B = 0;
for (let i = 0; i < n1; i++)
{
if (A[i] == '1' )
count1A++;
else
{
if ((count1A & 1) == 1)
odd1A++;
else
even1A++;
}
}
for (let i = 0; i < n2; i++)
{
if (B[i] == '1' )
count1B++;
else
{
if ((count1B & 1) == 1)
odd1B++;
else
even1B++;
}
}
if (count1A == count1B &&
odd1A == odd1B &&
even1A == even1B)
{
document.write( "Yes" );
}
else
document.write( "No" );
}
let A = "10011" , B = "11100" ;
canTransformStrings(A, B);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)