C++ simplest elegant O(n)


#1
    string Solution::addBinary(string A, string B) {
        if(A.length()<B.length())swap(A,B);
        reverse(A.begin(),A.end());
        reverse(B.begin(),B.end());
        int car=0;string s;
        for(int i=0;i<B.length();i++){
            int val=A[i]-'0'+B[i]-'0'+car;
            car=val/2;
            val=val&1;
            s+=(char)(val+'0');
        }
        for(int i=B.length();i<A.length();i++){
            int val=A[i]-'0'+car;
            car=val/2;
            val=val&1;
            s+=(char)(val+'0');
        }
        if(car==1)s+='1';
        reverse(s.begin(),s.end());
        return s;
    }