</>

43. Multiply Strings

Medium NeetCode 150
Math & Geometry Time O(M*N) Space O(M+N)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Math & Geometry
Time
O(M*N)
Space
O(M+N)

Standard Solution (C++)

SummaryGiven two non-negative integers represented as digit strings, compute their product and return it also as a string, without converting the entire strings to integers directly.

Approach
  • Simulate grade-school long multiplication: the digit at position i in num1 and position j in num2 contributes to position i + j + 1 in the result (result has at most m + n digits).
  • Accumulate all partial products into an (m + n)-length integer array, then propagate carries from right to left in a second pass.
  • Convert the array back to a string, skipping any leading zeros.
  • This avoids any big-integer parsing and works purely on digit arithmetic.
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    string multiply(string num1, string num2) {
        if (num1 == "0" || num2 == "0") return "0";

        int m = num1.size(), n = num2.size();
        // Product of m-digit and n-digit numbers has at most m+n digits.
        vector<int> digits(m + n, 0);

        // Accumulate partial products (right-to-left for both strings).
        for (int i = m - 1; i >= 0; --i) {
            for (int j = n - 1; j >= 0; --j) {
                digits[i + j + 1] += (num1[i] - '0') * (num2[j] - '0');
            }
        }

        // Propagate carries from the least-significant end toward the front.
        for (int k = m + n - 1; k > 0; --k) {
            digits[k - 1] += digits[k] / 10;
            digits[k]     %= 10;
        }

        // Build result string, skipping a possible leading zero at index 0.
        string result;
        result.reserve(m + n);
        int start = (digits[0] == 0) ? 1 : 0;
        for (int k = start; k < m + n; ++k)
            result += static_cast<char>('0' + digits[k]);

        return result;
    }
};
Time O(m * n) Space O(m + n)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    string multiply(string num1, string num2) {
        
        if(num1=="0" || num2=="0")
            return "0";
        
        int n1=num1.size();
        int n2=num2.size();

        vector<int> res(n1+n2, 0);

        for(int i=n1-1; i>=0; i--){
            for(int j=n2-1; j>=0; j--){
                res[i+j+1]+=(num1[i]-'0')*(num2[j]-'0');
                if(res[i+j+1]>=10){
                    res[i+j]+=(res[i+j+1]/10);
                    res[i+j+1]%=10;
                }
            }
        }

        int start=0;
        while(start<res.size() && res[start]==0)
            ++start;

        string str;
        for(int i=start; i<n1+n2; i++)
            str.push_back(res[i]+'0');

        return str;
    }
};

// Time Complexity: O(M*N)
// Space Complexity: O(M+N)
Time O(M*N) Space O(M+N)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern