> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Complex Number Multiplication Python Solution

> Tested Python solution for LeetCode 537 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 537, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/complex-number-multiply/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 537   # by problem number
lcpy gen -s complex_number_multiply   # by problem name
```

## Problem

A \<a href="[https://en.wikipedia.org/wiki/Complex\_number](https://en.wikipedia.org/wiki/Complex_number)" target="\_blank">complex number\</a> 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]`.
* `i^2 == -1`.

Given two complex numbers `num1` and `num2` as strings, return \<em>a string of the complex number that represents their multiplication\</em>.

### Examples

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

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

### Constraints

* `num1` and `num2` are valid complex numbers.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/complex_number_multiply/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/complex_number_multiply/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(num1) + len(num2))
    # Space: O(1)
    def complex_number_multiply(self, num1: str, num2: str) -> str:
        a, b = self._parse(num1)
        c, d = self._parse(num2)
        return f"{a * c - b * d}+{a * d + b * c}i"

    def _parse(self, num: str) -> tuple[int, int]:
        real, imag = num[:-1].split("+")
        return int(real), int(imag)
```

## Complexity

| Time | Space |
| - | - |
| O(len(num1) + len(num2)) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.