My issue
check my code
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t,l,k,h;
cin>>t;
while(t--)
{long int n ;
cin>>n ;
char a[n+1];
cin>>a;
char b[n+1];
cin>> b;
int i ,j,l=0;
if(j=='0'||j=='1')
{k=0,h=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]==j)
{ k++;}
}
for(i=0;b[i]!='\0';i++)
{
if( b[i]==j)
{h++;}
}
if(k!=h)
{l=1;}
}
if(l==0)
{cout<<"yes\n";
}else
{cout<<"no\n";}
}
}
Problem Link: Prime Reversal Practice Coding Problem
@ayushbansal07
plz refer the following code for better understanding of the logic
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s,s1;
cin>>s>>s1;
int o=0,z=0;
for(int i=0;i<n;i++)
{
if(s[i]=='1')
o++;
if(s1[i]=='1')
o--;
if(s[i]=='0')
z++;
if(s1[i]=='0')
z--;
}
if(o==0&&z==0)
cout<<"YES";
else
cout<<"NO";
cout<<endl;
}
return 0;
}
1 Like
The code checks whether the two binary strings contain the same number of 0s and 1s.
Logic
For each test case:
-
Read n, s, and s1.
-
Count the difference in the number of 1s between the two strings using o.
-
If s[i] == '1' → o++
-
If s1[i] == '1' → o--
-
Count the difference in the number of 0s between the two strings using z.
-
If s[i] == '0' → z++
-
If s1[i] == '0' → z--
-
If both o == 0 and z == 0, then both strings have identical counts of 0s and 1s, so print "YES".
-
Otherwise print "NO".
Simpler Observation
Since the strings are binary, checking only the count of 1s is sufficient:
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < n; i++) {
if(s[i] == '1') cnt1++;
if(s1[i] == '1') cnt2++;
}
if(cnt1 == cnt2)
cout << "YES\n";
else
cout << "NO\n";
Example
Input:
1
4
1100
1010
Counts:
-
First string: 2 ones, 2 zeros
-
Second string: 2 ones, 2 zeros
Output:
YES