Easy c++ solution. o(n)


#1

string add(string A,string B){
int l1=A.size(),l2=B.size();
int i=l1-1;
int f=l1-l2;
string ans;
int carry=0,sum=0;
while(i>=0){
if(i-f>=0)
carry+=(A[i]-‘0’)+(B[i-f]-‘0’);
else
carry+=(A[i]-‘0’);
ans+=(carry%2)+‘0’;
carry/=2;
i–;
}
if(carry!=0)
ans+=carry+‘0’;
reverse(ans.begin(),ans.end());
return ans;
}
string Solution::addBinary(string A, string B) {
string ans;
int l1=A.size(),l2=B.size();
if(l2>l1)
ans=add(B,A);
else
ans=add(A,B);
return ans;

}


#2

What a coincidence I had the exact same code :wink:


#3

:face_with_raised_eyebrow::face_with_raised_eyebrow::thinking::thinking: