Skip to content

LeetCode 537: Complex Number Multiplication

A clear explanation of multiplying complex numbers represented as strings using algebraic expansion.

Problem Restatement

We are given two strings representing complex numbers.

The format is:

"real+imaginaryi"

For example:

"1+1i"

represents:

1 + i

We need to multiply the two complex numbers and return the result in the same string format.

The imaginary unit satisfies:

i^2 = -1

Input and Output

ItemMeaning
InputTwo strings representing complex numbers
OutputTheir product as a complex-number string
Format"a+bi"
ValuesIntegers, possibly negative

Function shape:

def complexNumberMultiply(num1: str, num2: str) -> str:
    ...

Examples

Consider:

num1 = "1+1i"
num2 = "1+1i"

This means:

(1 + i)(1 + i)

Expand:

1 + i + i + i^2

Since:

i^2 = -1

we get:

1 + 2i - 1

So the result is:

0 + 2i

Output:

"0+2i"

Another example:

num1 = "1+-1i"
num2 = "1+-1i"

This means:

(1 - i)(1 - i)

Expand:

1 - i - i + i^2

Since:

i^2 = -1

we get:

1 - 2i - 1

So the result is:

-2i

Output:

"0+-2i"

First Thought: Parse and Use the Formula

A complex number:

a + bi

multiplied by:

c + di

follows standard algebra.

Expand:

(a+bi)(c+di)=ac+adi+bci+bdi2 (a+bi)(c+di)=ac+adi+bci+bdi^2

Since:

i2=1 i^2=-1

the last term becomes:

-bd

So the final form is:

(acbd)+(ad+bc)i (ac-bd)+(ad+bc)i

The only real work is parsing the input strings into integer components.

Key Insight

We do not need any advanced complex-number library.

The strings always follow the same structure:

real+imaginaryi

So we can:

  1. Remove the trailing "i".
  2. Split by "+".
  3. Convert both parts to integers.

Then directly apply the multiplication formula.

Algorithm

For each complex-number string:

  1. Remove the final "i".
  2. Split into real and imaginary parts.
  3. Convert both to integers.

Suppose:

num1 = a + bi
num2 = c + di

Compute:

real = ac - bd
imaginary = ad + bc

Return:

f"{real}+{imaginary}i"

Correctness

Suppose the parsed values are:

a + bi

and:

c + di

The multiplication of complex numbers follows ordinary distributive algebra:

(a + bi)(c + di)

Expanding gives:

ac + adi + bci + bdi^2

Since:

i^2 = -1

the expression becomes:

(ac - bd) + (ad + bc)i

The algorithm computes exactly these two quantities:

real = ac - bd
imaginary = ad + bc

and formats them back into the required string representation.

Therefore, the returned string represents the correct product of the two complex numbers.

Complexity

Let n be the length of the input strings.

MetricValueWhy
TimeO(n)Parsing the strings
SpaceO(1)Only a few integer variables

The arithmetic itself is constant time.

Implementation

class Solution:
    def complexNumberMultiply(self, num1: str, num2: str) -> str:
        def parse(s: str) -> tuple[int, int]:
            real, imaginary = s[:-1].split("+")
            return int(real), int(imaginary)

        a, b = parse(num1)
        c, d = parse(num2)

        real = a * c - b * d
        imaginary = a * d + b * c

        return f"{real}+{imaginary}i"

Code Explanation

The helper function parses one complex-number string.

We remove the trailing "i":

s[:-1]

For example:

"1+-1i"

becomes:

"1+-1"

Then we split by "+":

real, imaginary = s[:-1].split("+")

This gives:

["1", "-1"]

Then both parts become integers.

After parsing:

a, b = parse(num1)
c, d = parse(num2)

we compute the product formula:

real = a * c - b * d
imaginary = a * d + b * c

Finally, we rebuild the required format:

return f"{real}+{imaginary}i"

Manual Expansion Example

Suppose:

num1 = "2+3i"
num2 = "4+5i"

Parsed values:

VariableValue
a2
b3
c4
d5

Real part:

2 * 4 - 3 * 5
= 8 - 15
= -7

Imaginary part:

2 * 5 + 3 * 4
= 10 + 12
= 22

Result:

"-7+22i"

Testing

def run_tests():
    s = Solution()

    assert s.complexNumberMultiply("1+1i", "1+1i") == "0+2i"

    assert s.complexNumberMultiply("1+-1i", "1+-1i") == "0+-2i"

    assert s.complexNumberMultiply("2+3i", "4+5i") == "-7+22i"

    assert s.complexNumberMultiply("0+0i", "5+7i") == "0+0i"

    assert s.complexNumberMultiply("-1+-1i", "-1+-1i") == "0+2i"

    print("all tests passed")

run_tests()
TestWhy
(1+i)(1+i)Standard positive example
(1-i)(1-i)Checks negative imaginary part
Larger integersChecks general multiplication
Zero valueChecks multiplication by zero
Negative real and imaginary partsChecks sign handling