<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) {
        while (a.length() > b.length()) {
            b.insert(0, "0");
        }
        while (b.length() > a.length()) {
            a.insert(0, "0");
        }

        vector<char> aStr;
        vector<char> bStr;

        for (int i = 0; i < a.length(); i++) {
            aStr.push_back(a[i]);
        }
        for (int i = 0; i < b.length(); i++) {
            bStr.push_back(b[i]);
        }
        char carry = '0';
        vector<char> result;
        for (int i = aStr.size() - 1; i >= 0; i--) {
            if ((aStr[i] == '1' && bStr[i] == '0') ||
                (aStr[i] == '0' && bStr[i] == '1')) {
                if (carry == '0') {
                    result.push_back('1');
                    carry = '0';
                } else {
                    result.push_back('0');
                    carry = '1';
                }
            } else if (aStr[i] == '0' && bStr[i] == '0') {
                if (carry == '0') {
                    result.push_back('0');
                } else {
                    result.push_back('1');
                    carry = '0';
                }
            } else {
                if (aStr[i] == '1' && bStr[i] == '1') {
                    if (carry == '1') {
                        result.push_back('1');
                        carry = '1';
                    } else {
                        result.push_back('0');
                        carry = '1';
                    }
                }
            }
        }
        if (carry == '1') {
            result.push_back('1');
        }

        reverse(result.begin(), result.end());

        string str;
        for (int i = 0; i < result.size(); i++) {
            str.append(1, result[i]);
        }
        return str;
    }
};
8800