Simple Solution c++ using strings


#1

string Solution::addBinary(string A, string B) {
string sum="";
string c=“0”;
int n=A.size();
int m=B.size();
if(n>m)
{
int j=m;
while(j<n){
B=‘0’+B;
j++;
}
}
if(n<m)
{
int j=n;
while(j<m){
A=‘0’+A;
j++;
}
}
n=A.size();
for(int i=n-1;i>=0;i–){
int count=0;
if(A[i]==‘1’)
count++;
if(c==“1”)
count++;
if(B[i]==‘1’)
count++;
if(count==0){
A[i]=‘0’;
c=‘0’;
}
else if(count==1){
A[i]=‘1’;
c=‘0’;
}
else if(count==2){
A[i]=‘0’;
c=‘1’;
}
else
{
A[i]=‘1’;
c=‘1’;
}
}
if(c==“1”)
A=“1”+A;
return A;
}