Watch 10 video solutions for Multiply Strings, a medium level problem involving Math, String, Simulation. This walkthrough by NeetCode has 98,117 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 200num1 and num2 consist of digits only.num1 and num2 do not contain any leading zero, except the number 0 itself.Problem Overview: You receive two nonānegative integers as strings and must return their product as a string. Builtāin big integer libraries are not allowed, so the multiplication must be simulated digit by digit the same way manual multiplication works.
Approach 1: Elementary Schoolbook Multiplication (O(m*n) time, O(m+n) space)
This approach directly simulates how you multiply numbers on paper. Iterate from the last digit of each string, multiply digits, and place the result in a result array sized m + n. The key observation is that multiplying digits at positions i and j contributes to indices i + j and i + j + 1 in the result array. Each product adds to the current value, and the carry is propagated to the previous position. After processing all digit pairs, convert the array into a string while skipping leading zeros. This approach works well because multiplication of each digit pair is independent and fits perfectly with array accumulation.
The algorithm relies mostly on careful index management and carry handling. You iterate from right to left through both strings, compute (num1[i] - '0') * (num2[j] - '0'), and update the result array. Time complexity is O(m*n) since every digit pair is processed once, and space complexity is O(m+n) for the result storage. The logic is straightforward and commonly expected in interviews involving Math and String manipulation.
Approach 2: Optimized Schoolbook Multiplication with String Manipulation (O(m*n) time, O(m+n) space)
This version keeps the same multiplication principle but structures the computation to reduce intermediate conversions and simplify carry propagation. Instead of repeatedly converting characters to integers and performing multiple string operations, the digits are processed using preallocated arrays and controlled carry updates. Each digit multiplication immediately contributes to the correct position in the result buffer, minimizing temporary values and string concatenation overhead.
The improvement mainly affects implementation efficiency rather than asymptotic complexity. Time complexity remains O(m*n), and space complexity stays O(m+n). The code becomes cleaner when the result array is built first and converted to a string only once at the end. This approach is often described as a simulation of manual multiplication and is a common pattern in Simulation problems where arithmetic operations must be implemented explicitly.
Recommended for interviews: Interviewers expect the schoolbook multiplication simulation using a m + n sized result array. It demonstrates that you understand how digit positions interact during multiplication and how to manage carries correctly. A brute force stringāconcatenation style solution shows basic reasoning, but the arrayābased simulation demonstrates stronger control over indexing and space usage.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Elementary Schoolbook Multiplication | O(m*n) | O(m+n) | Standard interview solution when big integer libraries are not allowed |
| Optimized Schoolbook with Result Buffer | O(m*n) | O(m+n) | Cleaner implementation with fewer intermediate conversions and better string handling |