Home
DSA Patterns
Two Pointers
Multiply Strings
Multiply Strings (LeetCode 43)
Multiply Strings (LeetCode 43): multiply two numbers given as strings without converting them, using grade-school multiplication into an array of m + n digit positions.
Problem Understanding
Multiply Strings: given two non-negative integers num1 and num2 as strings, return their product as a string. You may not use a big-integer library or convert the inputs to integers directly.
Examples: "2" × "3" = "6", and "123" × "456" = "56088".
The Intuition: Every Digit Pair Has a Fixed Place
Long multiplication on paper multiplies every digit of one number by every digit of the other and adds the results in the right columns. Do the same with an array:
- The answer has at most
m + ndigits, so allocateposof that length. num1[i] × num2[j]belongs atpos[i + j + 1], with its tens carried topos[i + j].- Walk both strings from the right, so carries flow left into positions that are visited later.
Two indices sweeping a pair of strings from the end is the same shape as adding two numbers digit by digit, a cousin of the two pointers pattern. Leading zeros only appear at the front, as in Remove K Digits.
Multiply Strings Visualization
DIGIT i × DIGIT j LANDS ON pos[i + j + 1], CARRY TO pos[i + j]
m + n ZEROS
num1
num2
pos
0
1
2
3
4
5
product
:
answer
:
3 digits × 3 digits has at most 6 digits: pos = [0, 0, 0, 0, 0, 0]
Next
3 × 6 = 18, plus pos[5] 0 = 18: keep 8 at pos[5], carry 1 into pos[4]
num1
num2
num1 and num2 sit right-aligned above pos, the way you write long multiplication. Each step multiplies digits i and j; the ones position is solid and the carry position is tinted. Try All nines to see every position used.
Multiply Strings Dry Run
Input: num1 = "123", num2 = "456", so pos has 6 places.
| i, j | Digits | Product + pos[i+j+1] | pos after |
|---|---|---|---|
| 2, 2 | 3 × 6 | 18 + 0 = 18 | 0 0 0 0 1 8 |
| 2, 1 | 3 × 5 | 15 + 1 = 16 | 0 0 0 1 6 8 |
| 2, 0 | 3 × 4 | 12 + 1 = 13 | 0 0 1 3 6 8 |
| 1, 2 | 2 × 6 | 12 + 6 = 18 | 0 0 1 4 8 8 |
| 1, 1 | 2 × 5 | 10 + 4 = 14 | 0 0 2 4 8 8 |
| 1, 0 | 2 × 4 | 8 + 2 = 10 | 0 1 0 4 8 8 |
| 0, 2 | 1 × 6 | 6 + 4 = 10 | 0 1 1 0 8 8 |
| 0, 1 | 1 × 5 | 5 + 1 = 6 | 0 1 6 0 8 8 |
| 0, 0 | 1 × 4 | 4 + 1 = 5 | 0 5 6 0 8 8 |
Answer: 056088 without its leading zero: "56088".
Multiply Strings LeetCode Solution
One position array, filled from the right. A position can briefly hold more than 9; it is settled when the loop next uses it as the ones place.
Multiply Strings Code
1var multiply = function(num1, num2) {2 const m = num1.length, n = num2.length;3 const pos = Array(m + n).fill(0);4 for (let i = m - 1; i >= 0; i--) {5 for (let j = n - 1; j >= 0; j--) {6 const mul = Number(num1[i]) * Number(num2[j]);7 const total = mul + pos[i + j + 1];8 pos[i + j + 1] = total % 10;9 pos[i + j] += Math.floor(total / 10);10 }11 }12 const result = pos.join('').replace(/^0+/, '');13 return result || '0';14};
Multiply Strings in Python, Java, C++, TypeScript and C
Multiply Strings in Python
class Solution:
def multiply(self, num1: str, num2: str) -> str:
m, n = len(num1), len(num2)
pos = [0] * (m + n)
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
mul = int(num1[i]) * int(num2[j])
total = mul + pos[i + j + 1]
pos[i + j + 1] = total % 10
pos[i + j] += total // 10
result = ''.join(map(str, pos)).lstrip('0')
return result or '0'Multiply Strings in Java
class Solution {
public String multiply(String num1, String num2) {
int m = num1.length(), n = num2.length();
int[] pos = new int[m + n];
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
int total = mul + pos[i + j + 1];
pos[i + j + 1] = total % 10;
pos[i + j] += total / 10;
}
}
StringBuilder sb = new StringBuilder();
for (int d : pos) if (!(sb.length() == 0 && d == 0)) sb.append(d);
return sb.length() == 0 ? "0" : sb.toString();
}
}Multiply Strings in C++
class Solution {
public:
string multiply(string num1, string num2) {
int m = num1.size(), n = num2.size();
vector<int> pos(m + n, 0);
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
int mul = (num1[i] - '0') * (num2[j] - '0');
int total = mul + pos[i + j + 1];
pos[i + j + 1] = total % 10;
pos[i + j] += total / 10;
}
}
string result;
for (int d : pos) if (!(result.empty() && d == 0)) result.push_back('0' + d);
return result.empty() ? "0" : result;
}
};Multiply Strings in TypeScript
function multiply(num1: string, num2: string): string {
const m = num1.length, n = num2.length;
const pos: number[] = Array(m + n).fill(0);
for (let i = m - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
const mul = Number(num1[i]) * Number(num2[j]);
const total = mul + pos[i + j + 1];
pos[i + j + 1] = total % 10;
pos[i + j] += Math.floor(total / 10);
}
}
const result = pos.join('').replace(/^0+/, '');
return result || '0';
}Multiply Strings in C
char* multiply(char* num1, char* num2) {
int m = strlen(num1), n = strlen(num2);
int* pos = calloc(m + n, sizeof(int));
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
int mul = (num1[i] - '0') * (num2[j] - '0');
int total = mul + pos[i + j + 1];
pos[i + j + 1] = total % 10;
pos[i + j] += total / 10;
}
}
char* result = malloc(m + n + 1);
int k = 0;
for (int i = 0; i < m + n; i++)
if (!(k == 0 && pos[i] == 0)) result[k++] = '0' + pos[i];
if (k == 0) result[k++] = '0';
result[k] = '\0';
free(pos);
return result;
}Edge Cases & Common Mistakes
- A zero input: every position stays 0; return
"0", not an empty string. - Leading zeros: the product may have
m + n - 1digits; strip the front zero. - Off-by-one positions: the ones digit goes to
i + j + 1, the carry toi + j. - Converting to int or long: overflows for long inputs and is not allowed.
- Building the result by repeated string addition: correct but slower and far more code than the position array.
Summary
Allocate m + n positions. For every digit pair from the right, add the product to pos[i + j + 1], keep the last digit there and carry the rest to pos[i + j]. Join, strip leading zeros, and return "0" if nothing is left. O(m · n) time, O(m + n) space.
More practice: DSA interview questions with answers · patterns cheat sheet
