Skip to main content

Multiply Strings - Solution & Explanation

MediumMathStringSimulation25 min readAsked at: Amazon, Microsoft, Meta +14
Practice this problem

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 <= 200
  • num1 and num2 consist of digits only.
  • Both num1 and num2 do not contain any leading zero, except the number 0 itself.

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.

Try this approach in the editor →

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.

Code

Python

Java

Complexity

Time complexity: O(n * m), comparable to previous method.
Space complexity: O(n + m) but implemented to handle carry more elegantly.

Try this approach in the editor →

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 num1 and num2 both take the minimum value, then their product is {10}^{m - 1} times {10}^{n - 1} = {10}^{m + n - 2}, with a length of m + n - 1.
  • If num1 and num2 both 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 of m + 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

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Elementary Schoolbook Multiplication

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: Optimized Schoolbook Multiplication with String Manipulation

Time complexity: O(n * m), comparable to previous method.
Space complexity: O(n + m) but implemented to handle carry more elegantly.

Simulating Mathematical Multiplication

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Elementary Schoolbook MultiplicationO(m*n)O(m+n)Standard interview solution when big integer libraries are not allowed
Optimized Schoolbook with Result BufferO(m*n)O(m+n)Cleaner implementation with fewer intermediate conversions and better string handling

Video Solution

Multiply Strings - Leetcode 43 - PythonNeetCode102,620 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Multiply Strings easy or hard?
Multiply Strings is typically rated Medium difficulty. The logic is simple conceptually, but careful index management and carry handling make implementation error‑prone. Many candidates struggle with correctly mapping digit positions in the result array.
How to solve Multiply Strings in O(n)?
Achieving true O(n) time is not possible for the general case because every digit in one number interacts with every digit in the other during multiplication. The optimal practical solution is the schoolbook digit simulation with O(m*n) time. More advanced algorithms like Karatsuba exist but are rarely expected in coding interviews.
What is the best approach for Multiply Strings?
The best approach simulates elementary school multiplication using a result array of size m + n. Each pair of digits from the two strings is multiplied and placed in the correct position in the array while handling carry values. This method runs in O(m*n) time and O(m+n) space and avoids using built‑in big integer libraries.
What data structure is used in Multiply Strings?
Most solutions use an integer array of length m + n to store intermediate multiplication results. The array represents digit positions of the final number and allows easy carry propagation before converting the result back into a string.
What is the time complexity of Multiply Strings?
The time complexity is O(m*n), where m and n are the lengths of the two input strings. Every digit of the first number must be multiplied with every digit of the second number. Space complexity is O(m+n) because the final result can contain at most m + n digits.
Multiply Strings Python or Java solution approach?
Python and Java implementations follow the same algorithm: create an integer array of size m + n, multiply digits from right to left, store results at index i + j + 1, and propagate carry to i + j. After processing all digit pairs, convert the array to a string while skipping leading zeros.
Is Multiply Strings asked at Google, Amazon, or Meta?
Multiply Strings appears frequently in interviews at companies like Amazon, Google, Meta, and Microsoft because it tests low‑level number manipulation and string processing. Candidates must implement arithmetic without relying on big integer libraries.

Ready to solve this problem?

Practice Multiply Strings with our built-in code editor and test cases.

Practice on FleetCode