<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) {
        int carry = 0;
        int sum = 0;
        int a_int;
        int b_int;
        string answer = "";

        while((a.size() != 0 || b.size()!=0) || carry ==1){
            if (a.size() == 0){
                a_int = 0;
            }

            else{
                if (a[a.size()-1] == '1') a_int = 1;
                else a_int = 0;
            }


            if(b.size() == 0){
                b_int = 0;
            }
            else{
                if (b[b.size()-1] == '1') b_int = 1;
                else b_int = 0;                
            }


            sum = a_int + b_int + carry;

            if (sum>1){
                carry=1;
                sum = sum-2;
            }

            else{
                carry = 0;
            }

            if (a.size() != 0) a.pop_back();
            if (b.size() != 0) b.pop_back();

            if (sum == 0) answer = '0' + answer;
            if (sum == 1) answer = '1' + answer;
            
            //if (a.size() == 0 && b.size()==0 && carry ==1 ) answer = '1' + answer;
        }
        return answer;
    }
};
10900