<p>Given two non-negative integers <code>num1</code> and <code>num2</code> represented as strings, return the product of <code>num1</code> and <code>num2</code>, also represented as a string.</p>

<p><strong>Note:</strong>&nbsp;You must not use any built-in BigInteger library or convert the inputs to integer directly.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> num1 = "2", num2 = "3"
<strong>Output:</strong> "6"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> num1 = "123", num2 = "456"
<strong>Output:</strong> "56088"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num1.length, num2.length &lt;= 200</code></li>
	<li><code>num1</code> and <code>num2</code> consist of digits only.</li>
	<li>Both <code>num1</code> and <code>num2</code>&nbsp;do not contain any leading zero, except the number <code>0</code> itself.</li>
</ul>

class Solution {
public:
    string multiply(string num1, string num2) {
        if (num1.size() == 1 || num2.size() == 1) {
            return mult(num1, num2);
        }
        int m = max(num1.size(), num2.size())/2;
        string a, b, c, d;
        if (m >= num1.size()) {
            a = "0";
            b = num1;
        }
        else {
            a = num1.substr(0, num1.size()-m);
            b = num1.substr(num1.size()-m);
        }
        if (m >= num2.size()) {
            c = "0";
            d = num2;
        }
        else {
            c = num2.substr(0, num2.size()-m);
            d = num2.substr(num2.size()-m);
        }
        string z0 = multiply(b, d);
        string z3 = multiply(add(a, b), add(c, d));
        string z2 = multiply(a, c);
        string z1 = subtract(z3, add(z0, z2)) + string(m, '0');
        if (z2 != "0") {
            z2 += string(m*2, '0');
        }
        return add(z2, add(z1, z0));
    }

    string add(string num1, string num2) {
        int n = num1.size();
        int m = num2.size();
        string ans;
        bool carry = false;
        int i = 0;
        for (; i < min(n, m); i++) {
            int a = num1[n-1-i]-'0';
            int b = num2[m-1-i]-'0';
            int r = a + b + carry;
            if (r >= 10) {
                r -= 10;
                carry = true;
            } else {
                carry = false;
            }
            ans += (char)(r+'0');
        }
        if (i < n) {
            for (; i < n; i++) {
                int r = num1[n-1-i]-'0' + carry;
                if (r >= 10) {
                    r -= 10;
                    carry = true;
                } else {
                    carry = false;
                }
                ans += (char)(r+'0');
            }
        } else if (i < m) {
            for (; i < m; i++) {
                int r = num2[m-1-i]-'0' + carry;
                if (r >= 10) {
                    r -= 10;
                    carry = true;
                } else {
                    carry = false;
                }
                ans += (char)(r+'0');
            }
        }
        if (carry) {
            ans += "1";
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }

    string subtract(string num1, string num2) {
        int n = num1.size();
        int m = num2.size();
        string ans;
        bool borrow = false;
        int i = 0;
        for (; i < min(n, m); i++) {
            int a = num1[n-1-i]-'0';
            int b = num2[m-1-i]-'0';
            int r = a - b - borrow;
            if (r < 0) {
                r += 10;
                borrow = true;
            } else {
                borrow = false;
            }
            ans += (char)(r+'0');
        }
        if (i < n) {
            for (; i < n; i++) {
                int r = num1[n-1-i]-'0' - borrow;
                if (r < 0) {
                    r += 10;
                    borrow = true;
                } else {
                    borrow = false;
                }
                ans += (char)(r+'0');
            }
        } else if (i < m) {
            for (; i < m; i++) {
                int r = num2[m-1-i]-'0' - borrow;
                if (r < 0) {
                    r += 10;
                    borrow = true;
                } else {
                    borrow = false;
                }
                ans += (char)(r+'0');
            }
        }
        while (i > 0 && ans[i-1] == '0') i--;
        if (i == 0) return "0";
        ans = ans.substr(0, i);
        reverse(ans.begin(), ans.end());
        return ans;
    }

    string mult(string num1, string num2) {
        if (num1 == "0" || num2 == "0") return "0";
        if (num1.size() == 1) swap(num1, num2);
        int n = num2[0]-'0';
        string ans;
        int carry = 0;
        for (int i = num1.size()-1; i >= 0; i--) {
            int r = (num1[i]-'0') * n + carry;
            if (r >= 10) {
                carry = r / 10;
                r = r % 10;
            } else {
                carry = 0;
            }
            ans += (char)(r+'0');
        }
        if (carry) ans += (char)(carry+'0');
        reverse(ans.begin(), ans.end());
        return ans;
    }
};
15464