Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
class Solution {
public:
string addBinary(string a, string b) {
int len_a=a.length();
int len_b=b.length();
int carry=0,temp;
string res="";
int len=max(len_a,len_b);
for(int i=0;i<len;i++){
int p=(i<len_a)?a[len_a-1-i]-'0':0;
int q=(i<len_b)?b[len_b-1-i]-'0':0;
temp=p+q+carry;
carry=temp/2;
res.insert(res.begin(),temp%2+'0');
}