Multiply Strings

How to Multiply Strings Representing Large Numbers.

Multiplying two numbers is a straightforward operation, but what if the numbers are represented as strings? And what if their lengths are large enough to cause overflow when using conventional methods?

In this article, we’ll explore an efficient approach to solve the problem of multiplying strings representing non-negative integers without relying on built-in conversion functions.

Problem Description

You are given two non-negative integers as strings, num1 and num2. Your task is to compute their product and return the result as a string.

  • Constraints:
    • You cannot convert the strings directly into integers using built-in functions.
    • Neither num1 nor num2 will contain leading zeros, except if the value itself is 0.

Explanation:

  • Query = 2: The interval [2,3]  is the smallest one containing 2, it’s length is 2.
  • Query = 3: The interval [2,3]  is the smallest one containing 3, it’s length is 2.
  • Query = 1: The interval [1,3]  is the smallest one containing 1, it’s length is 3.
  • Query = 7: The interval [3,7]  is the smallest one containing 7, it’s length is 5.
  • Query = 6: The interval [6,6]  is the smallest one containing 6, it’s length is 1.
  • Query = 8: There is no interval containing 8.

Constraints:

  • 1 <= num1.length, num2.length <= 200 
  • num 1 and num 2 consist of digits only.

Steps to Solve

  1. Reverse the Strings:
    Start with the least significant digit (rightmost) to perform multiplication, just like in manual multiplication.

  2. Create an Array for Intermediate Results:
    Use an array of size len(num1)+len(num2) to store the results. This size ensures that the largest possible product fits.

  3. Multiply Digit by Digit:
    Multiply each digit of num1 with each digit of num2 and store the result in the appropriate position in the result array.

  4. Handle Carries:
    Adjust the result array for carries after all multiplications are done.

  5. Construct the Result String:
    Skip leading zeros in the result array and combine digits into the final output string.

Challenges in Multiplying Strings

  1. Handling Large Numbers:
    Since the strings can be up to 200 digits long, directly converting them to integers is not feasible due to potential overflow or performance issues.

  2. Simulating Manual Multiplication:
    Multiplication needs to be performed at the digit level, just like how we multiply numbers by hand on paper.

  3. Efficient Result Construction:
    Managing intermediate results and carrying over values without excessive computational overhead can be tricky.

There are mainly Four  approach to solve this problem – 

  1. Brute Force
  2. Sweep Line Algorithm
  3. Min Heap
  4. Min Segment Tree (Lazy Propagation)

1. Multiplication & Addition

  • Time complexity: O(min(m,n)∗(m+n))
  • Space complexity: O(m+n)

2. Multiplication

Time & Space Complexity
  • Time complexity: O(mn)
  • Space complexity: O(m+n)

Where m is the length of the array queries and n is the length of the array intervals.

More Articles