<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:
    bool buf[10005];
    string addBinary(string a, string b) {
        int sz = 0, sza = a.size(), szb = b.size();
        int maxsz = max(sza, szb);
        int last = 0;
        int calc = 0;
        for (int i = 1; i <= maxsz; ++i) {
            calc = last;
            if (sza >= i) {
                calc += ((a[sza - i] == '1') ? 1 : 0);
            }
            if (szb >= i) {
                calc += ((b[szb - i] == '1') ? 1 : 0);
            }
            last = (calc > 1);
            buf[sz++] = (calc % 2);
        }
        if (last > 0) {
            buf[sz++] = true;
        }
        string ret;
        ret.resize(sz);
        for (int i = 0; i < sz; ++i) {
            ret[sz - i - 1] = (buf[i] ? '1' : '0'); 
        }
        return ret;
    }
};
9100