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 are given two non‑negative integers as strings. The task is to multiply them and return the product as a string. Built‑in big integer libraries are not allowed, so you must simulate multiplication the same way humans do on paper.
Approach 1: Elementary Schoolbook Multiplication (O(m*n) time, O(m+n) space)
This approach directly simulates the multiplication method taught in elementary school. Iterate from the last digit of each string (least significant digit). For every pair of digits num1[i] and num2[j], compute their product and add it to a result array at position i + j + 1. The carry goes to i + j. Using an integer array of size m + n ensures every intermediate digit lands in the correct place value. After processing all digit pairs, convert the array to a string while skipping leading zeros.
This works because multiplying digits at indices i and j always contributes to positions based on their place values. Instead of constructing partial strings and shifting them, the algorithm writes the result directly into the correct slots. The algorithm uses basic math operations and digit manipulation with a time complexity of O(m*n) and space complexity of O(m+n). This is the most common and reliable approach used in interviews.
Approach 2: Optimized Schoolbook Multiplication with String Manipulation (O(m*n) time, O(m+n) space)
This variation still follows the schoolbook multiplication idea but focuses on cleaner digit handling and string construction. Instead of repeatedly converting characters during multiplication, digits are processed in a structured way while building partial results. Each digit of the second string multiplies the entire first string, producing a partial product that is shifted according to its position.
Instead of storing everything in a single array immediately, you accumulate intermediate results as strings or arrays and merge them using carry-aware addition. This approach emphasizes string manipulation and careful carry propagation. While the asymptotic complexity remains O(m*n), some implementations can be easier to reason about during debugging because each step mirrors the manual multiplication process.
This method also highlights the idea of simulation problems where you replicate a real-world process step by step. You explicitly model partial products, offsets, and carries, which helps reinforce how positional arithmetic works under the hood.
Recommended for interviews: The array-based schoolbook multiplication is the approach interviewers expect. It demonstrates understanding of place-value arithmetic, efficient carry handling, and avoiding expensive string concatenations. Showing the naive simulation first can communicate your reasoning, but implementing the optimized array solution proves you can translate mathematical insight into efficient code.
This approach mimics the multiplication process that is taught in schools. The idea is to multiply each digit of num1 with each digit of num2 and store the result in the corresponding position in an array. This intermediate result is then summed together to form the final product. We avoid using any built-in library functions for handling large numbers.
This Python solution uses an array (list) to store the intermediate results of each single-digit multiplication. We iterate backwards over both numbers, calculate the multiplication of individual digits, and manage the carry for each step. The results are stored from the least significant to the most significant position.
Java
C
C#
JavaScript
Time complexity: O(n * m), where n and m are the lengths of num1 and num2 respectively.
Space complexity: O(n + m) for the result array used to store intermediate results.
This approach leverages string manipulation and direct indexing to carefully manage carries and keep track of the multiplication progression similarly to approach one. The ultimate goal is to achieve better space optimization by minimizing the auxiliary storage.
Optimized through direct multiplication with preexisting carries within the innermost loop, this approach does not drastically alter the foundational logics compared to approach one, yet reduces redundancy in carry handling.
Java
Time complexity: O(n * m), comparable to previous method.
Space complexity: O(n + m) but implemented to handle carry more elegantly.
| Approach | Complexity |
|---|---|
| Approach 1: Elementary Schoolbook Multiplication | Time complexity: O(n * m), where n and m are the lengths of |
| Approach 2: Optimized Schoolbook Multiplication with String Manipulation | Time complexity: O(n * m), comparable to previous method. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Elementary Schoolbook Multiplication (Array) | O(m*n) | O(m+n) | Best general solution. Efficient and commonly expected in coding interviews. |
| Schoolbook Multiplication with String Manipulation | O(m*n) | O(m+n) | Useful for understanding manual multiplication or when building results incrementally. |
Multiply Strings - Leetcode 43 - Python • NeetCode • 78,995 views views
Watch 9 more video solutions →Practice Multiply Strings with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor