# LeetCode 537: Complex Number Multiplication

## Problem Restatement

We are given two strings representing complex numbers.

The format is:

```text
"real+imaginaryi"
```

For example:

```python
"1+1i"
```

represents:

```python
1 + i
```

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

The imaginary unit satisfies:

```python
i^2 = -1
```

## Input and Output

| Item | Meaning |
|---|---|
| Input | Two strings representing complex numbers |
| Output | Their product as a complex-number string |
| Format | `"a+bi"` |
| Values | Integers, possibly negative |

Function shape:

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

## Examples

Consider:

```python
num1 = "1+1i"
num2 = "1+1i"
```

This means:

```python
(1 + i)(1 + i)
```

Expand:

```python
1 + i + i + i^2
```

Since:

```python
i^2 = -1
```

we get:

```python
1 + 2i - 1
```

So the result is:

```python
0 + 2i
```

Output:

```python
"0+2i"
```

Another example:

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

This means:

```python
(1 - i)(1 - i)
```

Expand:

```python
1 - i - i + i^2
```

Since:

```python
i^2 = -1
```

we get:

```python
1 - 2i - 1
```

So the result is:

```python
-2i
```

Output:

```python
"0+-2i"
```

## First Thought: Parse and Use the Formula

A complex number:

```python
a + bi
```

multiplied by:

```python
c + di
```

follows standard algebra.

Expand:

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

Since:

$$
i^2=-1
$$

the last term becomes:

```python
-bd
```

So the final form is:

$$
(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:

```text
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:

```python
num1 = a + bi
num2 = c + di
```

Compute:

```python
real = ac - bd
imaginary = ad + bc
```

Return:

```python
f"{real}+{imaginary}i"
```

## Correctness

Suppose the parsed values are:

```python
a + bi
```

and:

```python
c + di
```

The multiplication of complex numbers follows ordinary distributive algebra:

```python
(a + bi)(c + di)
```

Expanding gives:

```python
ac + adi + bci + bdi^2
```

Since:

```python
i^2 = -1
```

the expression becomes:

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

The algorithm computes exactly these two quantities:

```python
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.

| Metric | Value | Why |
|---|---:|---|
| Time | `O(n)` | Parsing the strings |
| Space | `O(1)` | Only a few integer variables |

The arithmetic itself is constant time.

## Implementation

```python
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"`:

```python
s[:-1]
```

For example:

```python
"1+-1i"
```

becomes:

```python
"1+-1"
```

Then we split by `"+"`:

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

This gives:

```python
["1", "-1"]
```

Then both parts become integers.

After parsing:

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

we compute the product formula:

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

Finally, we rebuild the required format:

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

## Manual Expansion Example

Suppose:

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

Parsed values:

| Variable | Value |
|---|---:|
| `a` | `2` |
| `b` | `3` |
| `c` | `4` |
| `d` | `5` |

Real part:

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

Imaginary part:

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

Result:

```python
"-7+22i"
```

## Testing

```python
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()
```

| Test | Why |
|---|---|
| `(1+i)(1+i)` | Standard positive example |
| `(1-i)(1-i)` | Checks negative imaginary part |
| Larger integers | Checks general multiplication |
| Zero value | Checks multiplication by zero |
| Negative real and imaginary parts | Checks sign handling |

