Browse curriculum

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:

  1. The answer has at most m + n digits, so allocate pos of that length.
  2. num1[i] × num2[j] belongs at pos[i + j + 1], with its tens carried to pos[i + j].
  3. 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

1
2
3

num2

4
5
6

pos

0

0

0

1

0

2

0

3

0

4

0

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]

Free preview: the first 4 of 11 steps

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 - 1 digits; strip the front zero.
  • Off-by-one positions: the ones digit goes to i + j + 1, the carry to i + 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

The approach, step by step

  1. Allocate m + n positions

    pos = [0] * (m + n).

  2. Multiply every digit pair from the right

    For i from m - 1 down and j from n - 1 down: total = num1[i] · num2[j] + pos[i + j + 1]; pos[i + j + 1] = total % 10; pos[i + j] += total / 10.

  3. Build the string

    Join pos, drop leading zeros, and return "0" if nothing is left.

Frequently asked questions

Why does digit i times digit j go to position i + j + 1?

Index the result array from the left with length m + n. num1[i] is worth 10^(m - 1 - i) and num2[j] is worth 10^(n - 1 - j), so their product is worth 10^(m + n - 2 - i - j). In a left-indexed array of length m + n, that place is index i + j + 1; the carry goes one place left, to i + j.


Why is the result at most m + n digits?

num1 < 10^m and num2 < 10^n, so their product is less than 10^(m + n), which has at most m + n digits. It may have one fewer, which shows up as a leading zero in the array.


Why not convert the strings to integers?

The problem forbids it, and the inputs can be up to 200 digits, far beyond 64-bit integers. Working digit by digit keeps every intermediate value small.

New to the technique behind this problem? Watch it run step by step:

Two Pointers Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description