{x}
blog image

Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

 

Example 1:

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

Example 2:

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

 

Constraints:

  • 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.

Solution Explanation: Multiply Strings

This problem requires multiplying two large numbers represented as strings without using built-in big integer libraries. The solution uses a simulation of the standard multiplication algorithm we learn in elementary school.

Approach:

  1. Handle trivial cases: If either num1 or num2 is "0", the result is "0".

  2. Initialization: Create an integer array arr of size m + n, where m and n are the lengths of num1 and num2 respectively. This array will store the digits of the product. We use m + n because the maximum length of the product of two numbers with lengths m and n is m + n.

  3. Multiplication: Iterate through num1 from right to left (least significant digit to most significant digit). For each digit a in num1, iterate through num2 from right to left. For each digit b in num2, multiply a and b and add the result to the corresponding position in arr. The position is calculated as i + j + 1, where i is the index of the digit in num1 and j is the index in num2. We add 1 because the least significant digit of the product of a and b goes to the position i+j+1.

  4. Carry Handling: After the multiplication step, we need to handle carries. Iterate through arr from right to left. For each digit, divide it by 10. The remainder becomes the new digit, and the quotient is added as a carry to the next digit (to the left).

  5. Result Construction: Convert arr into a string. Remove any leading zeros.

Time Complexity: O(mn) - The nested loops in the multiplication step dominate the runtime. We perform approximately mn multiplications and additions.

Space Complexity: O(m+n) - The space used is dominated by the arr array, which has a size of m+n.

Code Examples (Python):

class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        if num1 == "0" or num2 == "0":
            return "0"
        m, n = len(num1), len(num2)
        arr = [0] * (m + n)
        for i in range(m - 1, -1, -1):
            a = int(num1[i])
            for j in range(n - 1, -1, -1):
                b = int(num2[j])
                arr[i + j + 1] += a * b
        for i in range(m + n - 1, 0, -1):
            arr[i - 1] += arr[i] // 10
            arr[i] %= 10
        i = 0 if arr[0] else 1
        return "".join(str(x) for x in arr[i:])
 

The code in other languages follows the same logic, only differing in syntax and data structure handling. The core algorithm remains consistent across all languages.