A clear explanation of multiplying complex numbers represented as strings using algebraic expansion.
Problem Restatement
We are given two strings representing complex numbers.
The format is:
"real+imaginaryi"For example:
"1+1i"represents:
1 + iWe need to multiply the two complex numbers and return the result in the same string format.
The imaginary unit satisfies:
i^2 = -1Input 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:
def complexNumberMultiply(num1: str, num2: str) -> str:
...Examples
Consider:
num1 = "1+1i"
num2 = "1+1i"This means:
(1 + i)(1 + i)Expand:
1 + i + i + i^2Since:
i^2 = -1we get:
1 + 2i - 1So the result is:
0 + 2iOutput:
"0+2i"Another example:
num1 = "1+-1i"
num2 = "1+-1i"This means:
(1 - i)(1 - i)Expand:
1 - i - i + i^2Since:
i^2 = -1we get:
1 - 2i - 1So the result is:
-2iOutput:
"0+-2i"First Thought: Parse and Use the Formula
A complex number:
a + bimultiplied by:
c + difollows standard algebra.
Expand:
Since:
the last term becomes:
-bdSo the final form is:
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:
real+imaginaryiSo we can:
- Remove the trailing
"i". - Split by
"+". - Convert both parts to integers.
Then directly apply the multiplication formula.
Algorithm
For each complex-number string:
- Remove the final
"i". - Split into real and imaginary parts.
- Convert both to integers.
Suppose:
num1 = a + bi
num2 = c + diCompute:
real = ac - bd
imaginary = ad + bcReturn:
f"{real}+{imaginary}i"Correctness
Suppose the parsed values are:
a + biand:
c + diThe multiplication of complex numbers follows ordinary distributive algebra:
(a + bi)(c + di)Expanding gives:
ac + adi + bci + bdi^2Since:
i^2 = -1the expression becomes:
(ac - bd) + (ad + bc)iThe algorithm computes exactly these two quantities:
real = ac - bd
imaginary = ad + bcand 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
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":
s[:-1]For example:
"1+-1i"becomes:
"1+-1"Then we split by "+":
real, imaginary = s[:-1].split("+")This gives:
["1", "-1"]Then both parts become integers.
After parsing:
a, b = parse(num1)
c, d = parse(num2)we compute the product formula:
real = a * c - b * d
imaginary = a * d + b * cFinally, we rebuild the required format:
return f"{real}+{imaginary}i"Manual Expansion Example
Suppose:
num1 = "2+3i"
num2 = "4+5i"Parsed values:
| Variable | Value |
|---|---|
a | 2 |
b | 3 |
c | 4 |
d | 5 |
Real part:
2 * 4 - 3 * 5
= 8 - 15
= -7Imaginary part:
2 * 5 + 3 * 4
= 10 + 12
= 22Result:
"-7+22i"Testing
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 |