---
# System prepended metadata

title: 537 Complex Number Multiplication
tags: [leetcode]

---

# 537 Complex Number Multiplication
###### tags: `leetcode`
## Problem Statement
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.
## Solution
- This problem is simplfied since it needs no determination of adding the ```pure imaginary```, ```pure real``` into account. 
- Split the string with the ```+``` to separate real and imaginary number
```cpp=
auto a= num1.find("+");
real[ind]= trans(num1.substr(0, a));
im[ind]= trans(num1.substr(a+ 1, string::npos));
```
- When determining the number, see the positive and negative sign of them and decide the split again
```cpp=
if (word[0]== '-')
    return (-1)* stoi(word.substr(1, string::npos));
else
    return stoi(word); 
```
- Then combine the numbers by multiplication.
```cpp=
return to_string(real[0]* real[1]- im[0]* im[1])+"+"+to_string(real[0]* im[1]+ im[0]* real[1])+"i";
```