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.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.
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.
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.
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.
Python
JavaScript
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.
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).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Parse and Multiply Components | Time Complexity: O(1) because the operations are constant-time. |
| Regular Expression Parsing | Time Complexity: O(1) since the regex operation is effectively constant time for fixed-size strings. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Parse and Multiply Components | O(n) | O(1) | Best general solution. Clear logic using split and integer conversion. |
| Regular Expression Parsing | O(n) | O(1) | Useful when regex is preferred for structured string extraction. |
Complex Number Multiplication | LeetCode • Knowledge Amplifier • 2,877 views views
Watch 9 more video solutions →Practice Complex Number Multiplication with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor