oneby7

Math Number tricks

92. Multiply Strings

Medium

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"

Output: "8316"

Explanation: 308 × 27 = 8316.

Example 2

Input: num1 = "0", num2 = "98765"

Output: "0"

Explanation: The product is zero, which is written as "0", not as a run of zeros.

Example 3

Input: num1 = "99999999999999999999" num2 = "99999999999999999999"

Output: "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.

num1 = "125", num2 = "40"

Constraints

  • 1 ≤ num1.length, num2.length ≤ 2000
  • num1 and num2 hold only the digits 0 to 9
  • Neither num1 nor num2 starts with a zero, except the number 0 itself

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.