Skip to main content

Complex Number Multiplication - Solution & Explanation

MediumMathStringSimulation12 min readAsked at: Amazon, Meta, Shopup
Practice this problem

Problem Statement

A complex number can be represented as a string on the form "real+imaginaryi" where:

  • real is the real part and is an integer in the range [-100, 100].
  • imaginary is the imaginary part and is an integer in the range [-100, 100].
  • i2 == -1.

Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.

 

Example 1:

Input: num1 = "1+1i", num2 = "1+1i"
Output: "0+2i"
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.

Example 2:

Input: num1 = "1+-1i", num2 = "1+-1i"
Output: "0+-2i"
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.

 

Constraints:

  • num1 and num2 are valid complex numbers.

Approach Overview

Problem Overview: You receive two complex numbers formatted as strings like "a+bi". The task is to multiply them and return the result in the same format. You must parse the real and imaginary components, apply the complex multiplication rule, and format the output string.

Complex multiplication follows a fixed formula: (a + bi)(c + di) = (ac − bd) + (ad + bc)i. The challenge is not the math itself but reliably extracting integers from the string representation.

Approach 1: Parse and Multiply Components (O(n) time, O(1) space)

The most straightforward approach is manual string parsing. Split the input around the '+' character to separate the real part and the imaginary component. Then strip the trailing 'i' and convert both parts to integers. Once you have a, b, c, and d, apply the formula (ac − bd) for the real part and (ad + bc) for the imaginary part. Finally, concatenate the result back into the "x+yi" format.

This approach uses basic string operations such as split(), substring slicing, and integer conversion. The parsing step scans each string once, giving O(n) time where n is the string length, and O(1) extra space. This method is simple, language‑agnostic, and easy to implement in C, C++, Java, Python, C#, and JavaScript.

Approach 2: Regular Expression Parsing (O(n) time, O(1) space)

Another clean option is extracting the real and imaginary values using a regular expression. A pattern like (-?\d+)\+(-?\d+)i captures both numeric components directly. Once matched, convert the captured groups into integers and apply the same multiplication formula.

This approach reduces manual string manipulation and keeps the parsing logic concise. The regex engine scans the string once, so the complexity remains O(n) time with O(1) additional space. It fits well in languages with strong regex support such as Python or JavaScript. The core computation still relies on simple math operations and basic simulation of the formula.

Recommended for interviews: The manual parsing approach is typically expected. Interviewers want to see that you understand the complex number formula and can reliably extract components from a formatted string. Regex parsing is equally correct but sometimes viewed as overkill for such a predictable format. Showing the formula implementation clearly demonstrates both string handling and mathematical reasoning.

Approach 1: Parse and Multiply Components

This approach involves parsing the given strings to separate the real and imaginary components of each complex number. Once separated, apply the distributive property of multiplication for complex numbers: (a+bi)(c+di) = ac + adi + bci + bdi^2. Substitute i^2 with -1 and then combine the real and imaginary parts for the final output string.

In this C solution, we use the sscanf function to parse the real and imaginary parts of the complex numbers. Then, we compute the real and imaginary components of the product using the multiplication formula for complex numbers. Finally, we format the result back into a string with sprintf.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) because the operations are constant-time.
Space Complexity: O(1) since it only uses a fixed amount of space for the variables and a small dynamic allocation for the output string.

Try this approach in the editor →

Approach 2: Regular Expression Parsing

In this approach, regular expressions are used to parse the complex number strings. This involves defining a regex pattern to capture the real and imaginary components. After extracting these components, multiplication is performed using the same algebraic rules, and the result is formatted for output.

Using re.match in Python with a regular expression captures the real and imaginary parts directly. The parsed integers are used for multiplication, with results formatted back into strings in the expected form using Python's f-string syntax.

Code

Python

JavaScript

Complexity

Time Complexity: O(1) since the regex operation is effectively constant time for fixed-size strings.
Space Complexity: O(1) involving fixed-size auxiliary storage for components and result.

Try this approach in the editor →

Approach 3: Simulation

We can convert the complex number string into its real part a and imaginary part b, and then use the formula for complex number multiplication (a_1 + b_1i) times (a_2 + b_2i) = (a_1a_2 - b_1b_2) + (a_1b_2 + a_2b_1)i to calculate the result.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Parse and Multiply Components

Time Complexity: O(1) because the operations are constant-time.
Space Complexity: O(1) since it only uses a fixed amount of space for the variables and a small dynamic allocation for the output string.

Regular Expression Parsing

Time Complexity: O(1) since the regex operation is effectively constant time for fixed-size strings.
Space Complexity: O(1) involving fixed-size auxiliary storage for components and result.

Simulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Parse and Multiply ComponentsO(n)O(1)Best general solution. Clear logic using split and integer conversion.
Regular Expression ParsingO(n)O(1)Useful when regex is preferred for structured string extraction.

Video Solution

Complex Number Multiplication | LeetCodeKnowledge Amplifier2,881 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Complex Number Multiplication easy or hard?
The problem is rated Medium because the math formula is simple but careful string parsing is required. Handling negative numbers, extracting the imaginary part correctly, and formatting the output string are the main points where mistakes happen.
Complex Number Multiplication Python/Java solution
Both Python and Java implementations follow the same idea: split the string at '+', remove the trailing 'i', convert values to integers, then compute (ac − bd) and (ad + bc). Python typically uses split() and slicing, while Java often uses split("\\+") and substring operations.
How to solve Complex Number Multiplication in O(n)?
Parse the string "a+bi" into two integers: the real part a and the imaginary part b. Repeat for the second number to obtain c and d. Compute the result using (ac − bd) for the real component and (ad + bc) for the imaginary component, then format the output as "x+yi".
What is the best approach for Complex Number Multiplication?
The most practical approach is parsing the real and imaginary parts using string split operations and then applying the formula (ac − bd) + (ad + bc)i. This runs in O(n) time for string parsing and O(1) space. It is straightforward, language‑agnostic, and commonly expected in coding interviews.
Is Complex Number Multiplication asked at Google/Amazon/Meta?
String parsing and math‑simulation problems similar to this appear in interviews at large tech companies, especially in early or mid‑level coding rounds. The problem tests attention to detail, correct parsing, and applying a known mathematical formula correctly.
What data structure is used in Complex Number Multiplication?
No advanced data structures are required. The solution primarily uses basic string manipulation, integer variables, and arithmetic operations. Some implementations optionally use regular expressions for structured string parsing.
What is the time complexity of Complex Number Multiplication?
The overall complexity is O(n) where n is the length of the input string. The algorithm scans each string once to extract the numeric components, followed by constant‑time arithmetic operations. Space complexity stays O(1) since only a few integer variables are stored.

Ready to solve this problem?

Practice Complex Number Multiplication with our built-in code editor and test cases.

Practice on FleetCode