class Solution {
public:
string addBinary(string a, string b) {
int index = 0, carry = 0, sum = 0, i =0;
string res = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
while(index < a.length() && index < b.length()) {
sum = int(a[index] - '0') + int(b[index] - '0') + carry;
res.push_back(char((sum % 2) + '0'));
carry = sum / 2;
++index;
}
if (index < a.length()) {
for (i = index; i < a.length(); i++) {
sum = int(a[i] - '0') + carry;
res.push_back(char((sum % 2) + '0'));
carry = sum / 2;
}
}
if (index < b.length()) {
for (i = index; i < b.length(); i++) {
sum = int(b[i] - '0') + carry;
res.push_back(char((sum % 2) + '0'));
carry = sum / 2;
}
}
if (carry) {
res.push_back(char(carry + '0'));
}
reverse(res.begin(), res.end());
return res;
}
};