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

    unsigned long long convertTolong(string s){
        unsigned long long val = 0, multiple = 1, n = s.length();
        for(unsigned long long i = 0; i < n; i++)
        {
            val += (s[n - i - 1]=='1'?1:0) * multiple;
            multiple *= 2;
        }
        return val;
    }

    string convertToBinary(unsigned long long x){
        string ans = "";
        unsigned long long rem = 1;
        while(x >= 1)
        {
            rem = x % 2;
            x /= 2;
            ans = to_string(rem) + ans;
        }
        return (ans.length() > 0 ? ans : "0");
    }


    string addBinary(string a, string b) {
        // unsigned long long a1 = convertTolong(a);
        // unsigned long long b1 = convertTolong(b);
        // cout << a1 << " " <<  b1 << endl;
        // string c1 = convertToBinary(a1 + b1);
        // cout << c1 << endl;
        
        string ans = "";
        char carry = '0';

        if (b.length() > a.length()){
            string temp = b; b = a; a = temp;
        }

        int m = a.length(), n = b.length();
        for(int i = m - 1; i >= 0; --i, --n){
            
            // second string is left
            if (n > 0){
                if (a[i] == '0'){
                    if (b[n - 1] == '0') {
                        ans  = carry + ans;
                        carry = '0';
                    }
                    else{
                        ans = (carry == '0' ? '1' : '0') + ans;
                    }
                } 
                else {
                    if (b[n - 1] == '0') {
                        ans = (carry == '0' ? '1' : '0') + ans;
                    }
                    else {
                        ans = carry + ans;
                        carry = '1';
                    }
                }
            }
            // second string is over
            else {
                if (a[i] == '0'){
                    ans  = carry + ans;
                    carry = '0';
                 } 
                else {
                    ans = (carry == '0' ? '1' : '0') + ans;
                }
            }
        }

        // Last carry
        return (carry == '0' ? ans : '1' + ans);
    }
};

// 111 : 7
// 11 : 3
// ? 10 -> 110

11300