43. Multiply Strings - Detailed Explanation
Problem Statement
You are given two non-negative numbers, num1
and num2
, represented as strings. Your task is to return the product of these numbers, also represented as a string. You are not allowed to use any built-in library for handling large integers or convert the inputs directly to integers.
Example 1
Input: num1 = "2", num2 = "3"
Output: "6"
Explanation: 2 multiplied by 3 equals 6.
Example 2
Input: num1 = "123", num2 = "456"
Output: "56088"
Explanation: Multiplying 123 by 456 gives 56088.
Example 3
Input: num1 = "0", num2 = "52"
Output: "0"
Explanation: Any number multiplied by 0 is 0.
Constraints:
-
Both
num1
andnum2
consist only of digits (0-9). -
They do not contain any leading zeros, except the number "0" itself.
-
(1 \leq \text{num1.length}, \text{num2.length} \leq 200)
Hints
-
Simulate School Multiplication:
Imagine multiplying the numbers as you would do on paper. Multiply each digit of one number by every digit of the other, taking care of the position of each digit. -
Use an Array for Intermediate Results:
Since the maximum length of the product of two numbers of length (m) and (n) is (m+n), create an array of size (m+n) to store the intermediate sums and manage the carry appropriately.
Approach 1: Simulation of Multiplication (School Algorithm)
Explanation
This approach simulates the traditional multiplication algorithm taught in school:
-
Initialize:
Let (m) and (n) be the lengths ofnum1
andnum2
respectively. Create an arrayresult
of size (m+n) initialized with zeros. -
Multiply Digit by Digit:
For each digit innum1
(starting from the rightmost digit), multiply it with every digit innum2
(also starting from the rightmost digit). The product of the digits at positions (i) and (j) will contribute to the positions (i+j) and (i+j+1) in theresult
array. -
Manage Carry:
Add the multiplication result to the current position in theresult
array and manage the carry by updating the next left position. -
Convert to String:
After processing all digits, skip any leading zeros in theresult
array and convert the remaining digits into a string.
Step-by-Step Walkthrough
-
Step 1:
Letnum1 = "123"
andnum2 = "456"
. Initialize an array of size (3 + 3 = 6):
result = [0, 0, 0, 0, 0, 0]
. -
Step 2:
Start with the rightmost digit ofnum1
('3') and multiply it with each digit ofnum2
. For instance, multiplying '3' and '6' gives 18. Add 18 to the appropriate position inresult
and update the carry. -
Step 3:
Continue this process for each digit, updating the array with sums and carries. -
Step 4:
After processing, if the array is[0, 5, 6, 0, 8, 8]
, remove any leading zeros and convert it to "56088".
Complexity Analysis
-
Time Complexity: (O(m \times n)), where (m) is the length of
num1
and (n) is the length ofnum2
. -
Space Complexity: (O(m + n)), used by the result array.
Approach 2: Alternative Brute Force (Not Recommended)
Explanation
A naive approach might involve converting the strings to integers, multiplying them, and converting the result back to a string. However, this approach is not allowed by the problem constraints since you must not use any built-in library for handling big integers or perform direct conversions.
Code Implementations
Python Code
Java Code
Common Mistakes and Edge Cases
-
Not Handling "0" Properly:
If either input is "0", the product must be "0". Always check for this condition at the beginning. -
Incorrect Carry Handling:
Failing to update the carry correctly when summing the product can lead to incorrect results. Always remember to add the carry to the next position. -
Leading Zeros in the Result:
The result array may have leading zeros after the multiplication loop. Ensure you remove them before converting the array to a string. -
Indexing Errors:
Be careful with array indices when mapping digit positions from the input strings to the result array. Off-by-one errors are common.
Alternative Variations
-
Multiplication in Different Bases:
Instead of base 10, you might be asked to multiply numbers in another base (e.g., binary multiplication). -
Addition of Strings:
A related problem involves adding two numbers represented as strings, which requires similar carry management. -
Division of Strings:
Another variation might require you to divide two numbers represented as strings without using built-in big integer libraries.
Related Problems
GET YOUR FREE
Coding Questions Catalog