Complex Number Multiplication: Parsing and Math
Complex Number Multiplication is LeetCode 537. You are given two complex numbers represented as strings in the form "a+bi", where a is the real part and b is the imaginary part. Return the result of their multiplication, also as a string in the same format.
Examples:
num1 = "1+1i",num2 = "1+1i"returns"0+2i"because(1+1i)(1+1i) = 1 + 1i + 1i + 1i^2 = 1 + 2i - 1 = 0 + 2i.num1 = "1+-1i",num2 = "1+-1i"returns"0+-2i"because(1-1i)(1-1i) = 1 - 1i - 1i + 1i^2 = 1 - 2i - 1 = 0 - 2i.
The problem
You need to parse two complex number strings, multiply them using the standard formula, and format the result back into a string. The format is always "a+bi" where a and b are integers. Note that negative imaginary parts appear as "1+-1i", not "1-1i" - the + is always present.
The key insight
Complex number multiplication follows the formula (a+bi)(c+di) = (ac-bd) + (ad+bc)i. This comes from distributing (FOIL) and remembering that i^2 = -1. The real challenge is not the math itself, but parsing the string format correctly to extract the integer values.
The + in the format "a+bi" is always present, even when b is negative (e.g., "1+-1i"). This means you can reliably split on "+" to separate the real and imaginary parts. Strip the trailing "i" from the second piece, and you have both integers.
The solution
def complexNumberMultiply(num1: str, num2: str) -> str:
a, b = num1.split("+")
b = b[:-1]
c, d = num2.split("+")
d = d[:-1]
a, b, c, d = int(a), int(b), int(c), int(d)
real = a * c - b * d
imag = a * d + b * c
return f"{real}+{imag}i"
Step-by-step walkthrough
Step 1: Parse "1+1i"
Split on "+" and strip the trailing "i". The real part is the substring before "+", and the imaginary part is the number before "i".
Step 2: Parse "1+1i"
Same parsing logic for the second complex number. We now have all four values: a=1, b=1, c=1, d=1.
Step 3: Compute real part (ac - bd)
Multiply the real parts together, subtract the product of the imaginary parts. This comes from FOIL expansion of (a+bi)(c+di).
Step 4: Compute imaginary part (ad + bc)
Cross-multiply: a times d, plus b times c. These are the two cross terms from the expansion.
Step 5: Format result as "0+2i"
Combine the real and imaginary parts into the required string format: "{real}+{imag}i".
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Parse and compute | O(n) | O(1) |
Time: O(n) where n is the length of the input strings. Splitting and parsing each string takes linear time. The arithmetic operations are all constant time since the values are bounded integers.
Space: O(1) extra space beyond the output string. You only store four integer variables and the result.
The building blocks
1. String parsing with split
The entire problem hinges on extracting integers from a structured string. split("+") breaks "1+1i" into ["1", "1i"]. Then slicing off the last character with [:-1] turns "1i" into "1". This split-and-slice pattern shows up whenever you need to parse a formatted string into its components - IP addresses, version numbers, coordinate pairs, and many other formats follow the same idea.
2. Complex number multiplication formula
The formula (a+bi)(c+di) = (ac-bd) + (ad+bc)i is a direct application of the distributive property combined with i^2 = -1. You do not need to implement a complex number class or handle any special math library. Four multiplications, one subtraction, and one addition give you both parts of the result. This same formula appears in signal processing and matrix rotation problems, so it is worth committing to memory.
Edge cases
- Negative numbers like
"-1+-1i": The format guarantees the+separator is always there. Splitting on+still works becauseint("-1")correctly parses the negative sign. No special handling needed. - Zero values:
"0+0i"is valid input. The multiplication formula naturally produces"0+0i"when all components are zero. No division-by-zero or special case to worry about. - Large numbers: The problem constrains values to the range [-100, 100], so intermediate products fit comfortably in standard integers. Products like
100 * 100 = 10000and sums like10000 + 10000 = 20000are well within bounds.
From understanding to recall
This problem looks easy once you see the solution, and it is. But the details matter when you are writing it from memory: remembering to split on "+", remembering to strip the "i" with [:-1], getting the formula right as ac - bd for the real part and ad + bc for the imaginary part. It is easy to mix up the signs or forget which terms get added versus subtracted.
Spaced repetition helps you lock in the parsing pattern and the formula together as one unit. After a few reps, you will not have to re-derive the multiplication formula or think about how to handle the string format. The whole solution becomes one fluid sequence: split, parse, multiply, format.
Related posts
- Multiply Strings - Another multiplication problem where the main challenge is working with numbers encoded as strings
- Add Strings - Digit-by-digit arithmetic on string-encoded numbers, the same parse-compute-format pattern
- String to Integer (atoi) - A deeper dive into parsing integers from strings, handling signs, whitespace, and overflow