#537
Complex Number Multiplication
pupil · 590 · lc medium +29 · verified · 73.2% accepted · 755 likes · top 83%
Description
A complex number is encoded as the string "real+imaginaryi" where:
- real is an integer in [-100, 100] representing the real component.
- imaginary is an integer in [-100, 100] representing the imaginary component.
- i2 == -1.
Given two such encoded strings num1 and num2, compute their product and return the result in the same string format.
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.
Code
1
2
3