Multiply Strings
Difficulty: Medium
You're given two non-negative numbers as digit strings. They may be too large for a normal numeric type. Return their product as a digit string.
You can't convert the strings to numbers and multiply, because the numbers may be too big for that to work.
Examples
Input: num1 = "2", num2 = "3"
Output: "6"
Input: num1 = "123", num2 = "456"
Output: "56088"
123 * 456 = 56088, computed digit by digit without ever converting the full strings to numbers.
Input: num1 = "0", num2 = "12"
Output: "0"
Constraints
1 <= num1.length, num2.length <= 200
num1 and num2 consist of digits only.
Neither num1 nor num2 has leading zeros, except num1 and num2 themselves being exactly "0".
Approach
This is grade-school multiplication on digit strings. One way: multiply the first number by one digit of the second at a time, right to left. Pad each partial product with trailing zeros for its place value, and add it to a running total with string addition. It's correct, but each addition redoes work on digits already settled.
A more direct approach skips the repeated additions. Multiplying digit num1[i] by digit num2[j] always affects exactly two positions of the answer (its own digit, plus a possible carry to the left), based only on i + j. Adding every digit pair's contribution straight into a results array by position builds the whole answer in one pass. Convert it to a string once at the end.
Solutions
Brute Force - Digit-by-Digit With String Addition
For each digit of num2 (right to left), multiply it by all of num1 to get a partial product. Pad it with trailing zeros for its place value and add it to a running total with string addition.
function multiply(num1, num2) {
if (num1 === "0" || num2 === "0") return "0";
let result = "0";
for (let i = num2.length - 1; i >= 0; i--) {
const digit = Number(num2[i]);
let carry = 0;
let partial = "";
for (let j = num1.length - 1; j >= 0; j--) {
const product = Number(num1[j]) * digit + carry;
partial = (product % 10) + partial;
carry = Math.floor(product / 10);
}
if (carry > 0) partial = carry + partial;
// Shift this partial product left by its place value
partial += "0".repeat(num2.length - 1 - i);
result = addStrings(result, partial);
}
return result;
}
function addStrings(a, b) {
let result = "";
let carry = 0;
let i = a.length - 1;
let j = b.length - 1;
while (i >= 0 || j >= 0 || carry > 0) {
const digitA = i >= 0 ? Number(a[i]) : 0;
const digitB = j >= 0 ? Number(b[j]) : 0;
const sum = digitA + digitB + carry;
result = (sum % 10) + result;
carry = Math.floor(sum / 10);
i--;
j--;
}
return result;
}Time: O(m · n · max(m, n)) - m·n for the digit multiplications, plus a string addition of length up to m+n after each of the n digits · Space: O(m + n)
Optimal - Accumulate Into a Positions Array
The product of num1[i] and num2[j] always lands on positions i+j (its carry) and i+j+1 (its ones digit). Add every digit pair's product into a results array at those positions, then join the array into the final string.
function multiply(num1, num2) {
if (num1 === "0" || num2 === "0") return "0";
const m = num1.length, n = num2.length;
const digits = new Array(m + n).fill(0);
for (let i = m - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
const product = Number(num1[i]) * Number(num2[j]);
const sumPos = i + j + 1; // ones place of this product
const carryPos = i + j; // tens place of this product
const total = digits[sumPos] + product;
digits[sumPos] = total % 10;
digits[carryPos] += Math.floor(total / 10);
}
}
// Skip a single leading zero, if the result didn't need the full width
let start = 0;
while (start < digits.length - 1 && digits[start] === 0) {
start++;
}
return digits.slice(start).join("");
}Time: O(m · n) · Space: O(m + n)