Complex numbers are mathematical objects that extend real numbers by introducing the imaginary unit i, where i² = -1. They're widely used in engineering, physics, and computer graphics for representing rotations and oscillations.
In this problem, you're given two complex numbers as strings in the format "a+bi" or "a-bi", where:
ais the real part (integer between -100 and 100)bis the imaginary part (integer between -100 and 100)irepresents the imaginary unit
Your task is to multiply these two complex numbers and return the result as a string in the same format.
Mathematical Formula: (a + bi) × (c + di) = (ac - bd) + (ad + bc)i
For example: "1+1i" × "1+1i" = "0+2i" because (1×1 - 1×1) + (1×1 + 1×1)i = 0 + 2i
Input & Output
Visualization
Time & Space Complexity
Where n is the length of the input strings. We need to parse each character once to extract components.
Only using a constant amount of extra space to store the parsed components and result.
Constraints
- The real and imaginary parts are integers in the range [-100, 100]
-
The input strings will always be in the format
"a+bi"or"a-bi" - The imaginary part will always end with the character 'i'
- Both input strings represent valid complex numbers