Multiply Strings - Solution & Explanation
Problem Statement
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 <= 200num1andnum2consist of digits only.- Both
num1andnum2do not contain any leading zero, except the number0itself.
Approach Overview
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 1: Approach 1: Elementary Schoolbook Multiplication
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.
Code
Python
Java
C
C#
JavaScript
Complexity
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.
Approach 2: Approach 2: Optimized Schoolbook Multiplication with String Manipulation
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.
Complexity
Time complexity: O(n * m), comparable to previous method.
Space complexity: O(n + m) but implemented to handle carry more elegantly.
Approach 3: Simulating Mathematical Multiplication
Assume the lengths of num1 and num2 are m and n respectively, then the length of their product can be at most m + n.
The proof is as follows:
- If
num1andnum2both take the minimum value, then their product is{10}^{m - 1} times {10}^{n - 1} = {10}^{m + n - 2}, with a length ofm + n - 1. - If
num1andnum2both take the maximum value, then their product is({10}^m - 1) times ({10}^n - 1) = {10}^{m + n} - {10}^m - {10}^n + 1, with a length ofm + n.
Therefore, we can apply for an array of length m + n to store each digit of the product.
From the least significant digit to the most significant digit, we calculate each digit of the product in turn, and finally convert the array into a string.
Note to check whether the most significant digit is 0, if it is, remove it.
The time complexity is O(m times n), and the space complexity is O(m + n). Here, m and n are the lengths of num1 and num2 respectively.
Code
Python
Java
C++
Go
TypeScript
Rust
C#
PHP
Kotlin
JavaScript
Complexity Comparison
| 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. |
| Simulating Mathematical Multiplication | — |
Detailed Complexity Analysis
| 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 |
Video Solution
Multiply Strings - Leetcode 43 - Python • NeetCode • 102,620 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Multiply Strings easy or hard?
How to solve Multiply Strings in O(n)?
What is the best approach for Multiply Strings?
What data structure is used in Multiply Strings?
What is the time complexity of Multiply Strings?
Multiply Strings Python or Java solution approach?
Is Multiply Strings asked at Google, Amazon, or Meta?
Ready to solve this problem?
Practice Multiply Strings with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor