43/670

43. Multiply Strings

Medium

Two non-negative integers arrive as strings, num1 and num2. They can be up to 200 digits long — far beyond what a 64-bit integer holds — so you must multiply them digit by digit, the way you would on paper. Return the product as a string with no leading zeros.

House rule: do not shortcut by converting the full strings with a built-in big-integer type (int(num1) in Python, BigInteger in Java). Single digits may of course become numbers.

Neither input has leading zeros, except when the number itself is "0".

Example 1:

Input: num1 = "2", num2 = "3"

Output: "6"

Explanation: 2 × 3 = 6.

Example 2:

Input: num1 = "123", num2 = "456"

Output: "56088"

Explanation: 123 × 456 = 56088, built from digit products and carries.

Example 3:

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

Output: "0"

Explanation: Anything times zero is "0" — a single zero, not "000".

Constraints:

  • 1 ≤ num1.length, num2.length ≤ 200
  • num1 and num2 consist of digits 0-9 only
  • No leading zeros, except the number "0" itself

Hints:

On paper you multiply num1 by each digit of num2, shift each partial product one place further left, and add the rows. That translates directly into string helpers: multiply-by-one-digit and add-two-strings.

Slicker: digit i of num1 times digit j of num2 lands in output position i + j + 1 (with a carry into i + j). An int array of size len(num1) + len(num2) collects all products; one carry pass at the end normalizes it.

Handle the zero product early or strip leading zeros at the end — the positions array for "0" × "999" is all zeros.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: num1 = "2", num2 = "3"

Expected output: "6"