LeetCode #43 Medium

Multiply Strings

Multiply Strings is LeetCode 43 (Medium). Two non-negative integers arrive as strings num1 and num2. Return their product, also as a string.

  • No built-in big-integer library, and no converting the inputs straight to integers.
  • Inputs have no leading zeros, except the number 0 itself.

Each string can be 200 digits long, far beyond any 64-bit integer, so the multiplication has to happen digit by digit.

Constraints
  • 1 <= num1.length, num2.length <= 200
  • num1 and num2 consist of digits only.
  • Both num1 and num2 do not contain any leading zero, except the number 0 itself.
mathstringssimulation
Open on LeetCode ↗
02

Intuition

This is school long multiplication, but without writing out the shifted rows and adding them at the end. The product of digit num1[i] and digit num2[j] always lands in the same place value, so it can be added into a fixed slot right away.

  • Where it lands: an m-digit number times an n-digit number has at most m + n digits. With slot 0 as the highest place, the pair (i, j) belongs in slot i + j + 1.
  • Carrying: after adding, the slot keeps the last digit and passes the tens to slot i + j, one place to the left.

Working right to left, every slot is settled before anything to its left needs it.

How to spot this pattern

Arithmetic on numbers given as strings or digit lists, too large for built-in types: simulate the school method with one digit per slot. Add Strings and Add Binary do the same for addition, and Plus One for a single increment.

03

Approach

Try it first

Before reading on, multiply 12 by 34 on paper and note which column each digit product (2×4, 2×3, 1×4, 1×3) ends up in. Aim for an O(m·n) loop with one array of m + n digits.

1

Return early for zero

If either string is "0", return "0". Every other input is free of leading zeros, which keeps the final clean-up to a single digit.

2

Make one slot per place value

Create pos, an array of m + n zeros. The product cannot be longer, because a number below 10^m times a number below 10^n is below 10^(m+n).

3

Add each digit product into its slot

Loop i from the last digit of num1 and, inside, j from the last digit of num2. Compute total = num1[i] * num2[j] + pos[i + j + 1], store total % 10 back in that slot, and add total // 10 to pos[i + j].

4

Why the slots end up as single digits

A slot may hold more than 9 for a moment after receiving a carry. The next product that lands there adds to it and splits it again, and the leftmost carry only reaches slot 0 at the very end, when the true product fits.

5

Drop at most one leading zero

Both inputs are non-zero, so the product has either m + n or m + n - 1 digits. Skip slot 0 if it is 0 and join the rest into the answer string.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def multiply(self, num1, num2):
▶3 if num1 == "0" or num2 == "0":
▶4 return "0"
▶5 m, n = len(num1), len(num2)
▶6 pos = [0] * (m + n)
▶7 for i in range(m - 1, -1, -1):
▶8 for j in range(n - 1, -1, -1):
▶9 total = int(num1[i]) * int(num2[j]) + pos[i + j + 1]
▶10 pos[i + j + 1] = total % 10
▶11 pos[i + j] += total // 10
▶12 start = 1 if pos[0] == 0 else 0
▶13 return "".join(map(str, pos[start:]))
num1123num2×456slots000000product×+=6 slots: the product has at most 6 digits
pos[0, 0, 0, 0, 0, 0]one slot per place value
A 3-digit number times a 3-digit number has at most 6 digits, so 6 slots of 0 are enough. Slot 0 is the highest place value and the last slot is the ones place.
num1123num2×456slots00001carry8digitproduct3×6+0=18in slot3 times 6 plus 0 is 18 → keep 8, carry 1
i, j2, 2
pos[0, 0, 0, 0, 1, 8]
Digit 3 sits at place 0 and digit 6 at place 0 (counting from the right), so their product belongs in slot 5. Adding the 0 already there gives 18: slot 5 keeps 8 and the 1 moves one place left, into slot 4.
num1123num2×456slots0001carry6digit8product3×5+1=16in slot3 times 5 plus 1 is 16 → keep 6, carry 1
i, j2, 1
pos[0, 0, 0, 1, 6, 8]
Digit 3 sits at place 0 and digit 5 at place 1 (counting from the right), so their product belongs in slot 4. Adding the 1 already there gives 16: slot 4 keeps 6 and the 1 moves one place left, into slot 3.
num1123num2×456slots001carry3digit68product3×4+1=13in slot3 times 4 plus 1 is 13 → keep 3, carry 1
i, j2, 0
pos[0, 0, 1, 3, 6, 8]
Digit 3 sits at place 0 and digit 4 at place 2 (counting from the right), so their product belongs in slot 3. Adding the 1 already there gives 13: slot 3 keeps 3 and the 1 moves one place left, into slot 2.
num1123num2×456slots0014carry8digit8product2×6+6=18in slot2 times 6 plus 6 is 18 → keep 8, carry 1
i, j1, 2
pos[0, 0, 1, 4, 8, 8]
Digit 2 sits at place 1 and digit 6 at place 0 (counting from the right), so their product belongs in slot 4. Adding the 6 already there gives 18: slot 4 keeps 8 and the 1 moves one place left, into slot 3.
num1123num2×456slots002carry4digit88product2×5+4=14in slot2 times 5 plus 4 is 14 → keep 4, carry 1
i, j1, 1
pos[0, 0, 2, 4, 8, 8]
Digit 2 sits at place 1 and digit 5 at place 1 (counting from the right), so their product belongs in slot 3. Adding the 4 already there gives 14: slot 3 keeps 4 and the 1 moves one place left, into slot 2.
num1123num2×456slots01carry0digit488product2×4+2=10in slot2 times 4 plus 2 is 10 → keep 0, carry 1
i, j1, 0
pos[0, 1, 0, 4, 8, 8]
Digit 2 sits at place 1 and digit 4 at place 2 (counting from the right), so their product belongs in slot 2. Adding the 2 already there gives 10: slot 2 keeps 0 and the 1 moves one place left, into slot 1.
num1123num2×456slots011carry0digit88product1×6+4=10in slot1 times 6 plus 4 is 10 → keep 0, carry 1
i, j0, 2
pos[0, 1, 1, 0, 8, 8]
Digit 1 sits at place 2 and digit 6 at place 0 (counting from the right), so their product belongs in slot 3. Adding the 4 already there gives 10: slot 3 keeps 0 and the 1 moves one place left, into slot 2.
num1123num2×456slots01carry6digit088product1×5+1=6in slot1 times 5 plus 1 is 6 → keep 6, carry 0
i, j0, 1
pos[0, 1, 6, 0, 8, 8]
Digit 1 sits at place 2 and digit 5 at place 1 (counting from the right), so their product belongs in slot 2. Adding the 1 already there gives 6: slot 2 keeps 6 and the 0 moves one place left, into slot 1.
num1123num2×456slots0carry5digit6088product1×4+1=5in slot1 times 4 plus 1 is 5 → keep 5, carry 0
i, j0, 0
pos[0, 5, 6, 0, 8, 8]
Digit 1 sits at place 2 and digit 4 at place 2 (counting from the right), so their product belongs in slot 1. Adding the 1 already there gives 5: slot 1 keeps 5 and the 0 moves one place left, into slot 0.
num1123num2×456slots056088product56088slot 0 is 0 → drop it, answer: 56088
result"56088"
The product came out one digit shorter than the maximum, so slot 0 never received a carry. Dropping that single leading zero gives 56088. Only slot 0 can be zero here, because neither input is 0.
05

Common pitfalls

Converting the strings to numbers

✗ Wrong
return str(int(num1) * int(num2))
✓ Right
Multiply digit by digit into an array of m + n slots.

The problem forbids it, and in C++ or Java it overflows: "123456789" × "987654321" is about 1.2 × 10¹⁷, which wraps around in a 32-bit int, and 200-digit inputs overflow any built-in type.

Putting the product in slot i + j

✗ Wrong
total = d1 * d2 + pos[i + j]
pos[i + j] = total % 10
pos[i + j - 1] += total // 10
✓ Right
total = d1 * d2 + pos[i + j + 1]
pos[i + j + 1] = total % 10
pos[i + j] += total // 10

Slot i + j is one place too far left. For "2" × "3" the 6 lands in slot 0 and the carry goes to slot −1, which Python quietly reads as the last slot, so the answer comes out as "60".

Not handling a zero input

✗ Wrong
# no early return
return "".join(map(str, pos)).lstrip("0")
✓ Right
if num1 == "0" or num2 == "0":
    return "0"

For "0" × "52" every slot is 0, so stripping zeros leaves an empty string instead of "0"; stripping only the first slot leaves "00".

06

Complexity

Time
O(m · n)
Space
O(m + n)
Every digit of num1 meets every digit of num2 once. The slot array holds one digit per place of the product.
07

Multiply Strings FAQ

Is there anything faster than O(m · n)?

Yes. Karatsuba multiplication splits each number in half and needs three half-size products instead of four, about O(n^1.585). FFT-based methods reach about O(n log n). At 200 digits the simple loop is faster in practice, so interviews expect it.

How would negative numbers change the code?

Strip a leading - from each input and remember whether exactly one of them had it. Multiply the digits as usual, then put a - in front of the result if the signs differed and the result is not "0".