<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> a = "11", b = "1"
<strong>Output:</strong> "100"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> a = "1010", b = "1011"
<strong>Output:</strong> "10101"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= a.length, b.length &lt;= 10<sup>4</sup></code></li>
	<li><code>a</code> and <code>b</code> consist&nbsp;only of <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code> characters.</li>
	<li>Each string does not contain leading zeros except for the zero itself.</li>
</ul>

#include <bits/stdc++.h> 
using namespace std; 
class Solution {
public:
    string addBinary(string a, string b) {
  
        if(a.length()>b.length())
        return addBinary(b,a);

        int diff =b.length()-a.length();

        string padding ;
        for(int i =0;i<diff;i++)
        {
            padding.push_back('0');
        }
        a=padding+a;
        string res;
        char carry='0';
        for(int i =a.length()-1;i>=0;i--)
        {
            if(a[i]=='1'&&b[i]=='1')
            {
                if(carry=='1')
                res.push_back('1'),carry='1';
                else
                res.push_back('0'),carry='1';
            }else
            if(a[i]=='0'&&b[i]=='0')
            {
                if(carry=='1')
                res.push_back('1'),carry='0';
                else
                res.push_back('0'),carry='0';
            }else
            if(a[i]!=b[i])
            {
                if(carry=='1')
                res.push_back('0'),carry='1';
                else
                res.push_back('1'),carry='0';
            }
        }


            if(carry=='1')
            {
                res.push_back(carry);
            }
             reverse(res.begin(), res.end()); 
  
    int index = 0; 
    while (index + 1 < res.length() &&  
           res[index] == '0') 
        index++; 
    return (res.substr(index)); 
 
  
    }
        
};
        

8700