<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 result = "";
        string one = "1";
        string zero = "0";
        bool carryFlag = false;
        int i = 0;
        bool  aBit, bBit;
        aBit = false;
        bBit = false;
        int alength = a.length();
        int blength = b.length();
        while(i<a.length() || i< b.length()) {
            if(i<a.length()) {
                if(a[alength - i -1] == '1') {
                    aBit = true;
                }
                else {
                    aBit =  false;
                }
            }
            if(i<b.length()) {
                if(b[blength - i -1] == '1') {
                    bBit = true;
                }
                else {
                    bBit =  false;
                }
            }
            if(aBit && bBit) {
                if(carryFlag) {
                   // auto one = "1";
                   // result.append("1",0,1);
                    result = one.append(result);
                    carryFlag = false;
                }
                else {
                    //result.append("0",0,1);
                    result = zero.append(result);
                }
                carryFlag = true;
            }
            else if(!aBit && !bBit) {
                if(carryFlag) {
                    //result.append("1",0,1);
                    result = one.append(result);
                    carryFlag = false;
                }
                else {
                   // result.append("0",0,1);
                    result = zero.append(result);
                }
            }
            else {
                if(carryFlag) {
                   // result.append("0",0,1);
                    result = zero.append(result);
                    carryFlag = true;
                }
                else {
                    //result.append("1",0,1);
                    result = one.append(result);
                }
            }
            ++i;
            aBit = false;
            bBit = false;
            one = "1";
            zero = "0";

        }
        if(carryFlag) {
            //result.append("1",0,1);
            result = one.append(result);
            //result = "1".append(result);
        }
        return result;
    }
};
9300