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
0itself.
Each string can be 200 digits long, far beyond any 64-bit integer, so the multiplication has to happen digit by digit.
- 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.
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 ann-digit number has at mostm + ndigits. With slot 0 as the highest place, the pair(i, j)belongs in sloti + 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.
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.
Approach
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.
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.
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).
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].
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.
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.
Code in Python, C++ and Java
Common pitfalls
Converting the strings to numbers
return str(int(num1) * int(num2))
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
total = d1 * d2 + pos[i + j] pos[i + j] = total % 10 pos[i + j - 1] += total // 10
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
# no early return
return "".join(map(str, pos)).lstrip("0")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".
Complexity
num1 meets every digit of num2 once. The slot array holds one digit per place of the product.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".