<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 add(string a, string b) {
        int ptr = 0;
        int a_len = a.length(), b_len = b.length();
        int carry = 0;
        string res = "";
        while (ptr < min(b_len, a_len)) {
            int sum = carry + int(a[a_len - ptr - 1] - '0') + int(b[b_len - ptr - 1] - '0');
            if (sum > 9) { carry = 1; sum -= 10; }
            else carry = 0;
            res += char(sum + '0'); 
            ptr++;
        }

        string remaining = ptr >= a_len ? b : a;
        int r_len = remaining.length();
        while(ptr < r_len) {
            int sum = carry + int(remaining[r_len - ptr - 1] - '0');
            if (sum > 9) { carry = 1; sum -= 10; }
            else carry = 0;
            res += char(sum + '0'); 
            ptr++;
        }

        if (carry > 0) res += '1';

        reverse(res.begin(), res.end());
        return res;
    }

    string multiplyOne(string a, char b) {
        int carry = 0;
        int digit = b - '0';
        string res = "";
        for (int i = a.length() - 1; i >= 0; i--) {
            int ans = carry + int(a[i] - '0') * digit;
            if (ans > 9) { carry = ans/10; ans %= 10; }
            else carry = 0;
            res += char(ans + '0');
        }

        if (carry > 0) res += char(carry + '0');

        reverse(res.begin(), res.end());
        return res;
    }

    bool isZero(string s) {
        return s.length() == 1 && s[0] == '0';
    }

    string multiply(string num1, string num2) {
        if (isZero(num1) || isZero(num2)) return "0";
        
        int ptr = 0;
        unordered_map<char, string> mp;
        string ans = "0";
        int a_len = num1.length();
        while( ptr < a_len ) {
            string curr = "";
            char curr_digit = num1[a_len - ptr - 1];
            if (mp.find(curr_digit) != mp.end()) {
                curr = mp[curr_digit];
            }
            else {
                curr = multiplyOne(num2, curr_digit);
                cout << curr << endl;
                mp[curr_digit] = curr;
            }
            for(int i = 0; i < ptr; i++) curr += '0';
            ans = add(ans, curr);
            ptr++;
        } 

        return ans; 
    }
};
16390