<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>

class Solution {
public:
    string addBinary(string a, string b) {
        string binarySum = "";
        char c = '0';
        int l1 = a.size(), l2 = b.size(), l = max(l1, l2);

        if(l1 < l) {
            for(int i = 0; i < l-l1; i++) {
                a = '0' + a;
            }
        }

        if(l2 < l) {
            for(int i = 0; i < l-l2; i++) {
                b = '0' + b;
            }
        }

        for(int i = l-1; i >= 0; i--) {
            if(a[i] == '1' && b[i] == '1') {
                if(c == '0') {
                    binarySum = '0' + binarySum;                    
                }
                else {
                    binarySum = '1' + binarySum; 
                }

                c = '1';
            }
            else if(a[i] == '1' || b[i] == '1') {
                if(c == '0') {
                    binarySum = '1' + binarySum;   
                    c = '0';                  
                }
                else {
                    binarySum = '0' + binarySum; 
                    c = '1'; 
                }            
            }
            else {
                if(c == '0') {
                    binarySum = '0' + binarySum;               
                }
                else {
                    binarySum = '1' + binarySum;                     
                }    
                c = '0'; 
            }
        }

        if(c == '1') {
            return c + binarySum;
        }
        else {
            return binarySum;
        }
    }
};
12500