Watch 10 video solutions for Complex Number Multiplication, a medium level problem involving Math, String, Simulation. This walkthrough by Knowledge Amplifier has 2,877 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |