Math Number tricks
92. Multiply Strings
You are given two non-negative integers, num1 and num2, written as strings of decimal digits. Return their
product, written the same way.
The numbers can be far longer than any built-in integer type holds. Work on the digits yourself: do not turn
a whole string into a number, and do not use a big-number type such as BigInteger, BigInt or Python's
int of the whole string.
The answer must not start with a zero, unless the product is zero, which is written as "0". Answers are
shown in double quotes.
Example 1
Input: num1 = "308", num2 = "27"
"8316"Explanation: 308 × 27 = 8316.
Example 2
Input: num1 = "0", num2 = "98765"
"0"Explanation: The product is zero, which is written as "0", not as a run of zeros.
Example 3
Input: num1 = "99999999999999999999"
num2 = "99999999999999999999"
"9999999999999999999800000000000000000001"Explanation: Each number has 20 digits, more than a 64-bit integer holds. Their product has 40 digits.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ num1.length, num2.length ≤ 2000num1andnum2hold only the digits 0 to 9- Neither
num1nornum2starts with a zero, except the number 0 itself
Clarifying questions
What you would ask an interviewer before writing any code.