Multiply Strings
Medium · Math · Math · String · Simulation
Problem
You are given two non-negative integers num1 and num2 written as decimal strings. Return their product, also as a string.
Do not use a built-in big-integer type and do not convert the whole inputs to integers directly. Work digit by digit, as in long multiplication.
Examples
Input: num1 = "12", num2 = "34" Output: "408"
12 × 34 = 408.
Input: num1 = "999", num2 = "0" Output: "0"
Anything times zero is "0", not "000".
Input: num1 = "250", num2 = "4000" Output: "1000000"
250 × 4000 = 1,000,000.
Constraints
- 1 <= len(num1), len(num2) <= 200
- num1 and num2 contain digits only
- Neither input has leading zeros, except the number "0" itself
Approaches
- Partial products added as strings — time O(n · (m + n)), space O(m + n)
Follow-up
For numbers with many thousands of digits, O(m · n) becomes the bottleneck. Karatsuba multiplication splits each number in half and needs three half-size products instead of four, for about O(n^1.585); FFT-based methods go further, to about O(n log n).
Related problems
Start free on Cariara · Coding problems · System design studio