Complex Number Multiplication
MedPrompt
A complex number can be represented as a string on the form "real+imaginaryi" where:
realis the real part and is an integer in the range[-100, 100].imaginaryis 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:
num1andnum2are valid complex numbers.
Approaches
3 approaches with complexity analysis and trade-offs.
This approach uses regular expressions to parse the real and imaginary parts from the input strings. A regex pattern is defined to match the complex number format, and a matcher is used to extract the numeric values.
Algorithm
-
- Define a regex pattern
(-?\d+)\+(-?\d+)ito capture the real and imaginary parts.
- Define a regex pattern
-
- For
num1, create aMatcherand extract the two captured groups.
- For
-
- Convert the captured string groups to integers
aandb.
- Convert the captured string groups to integers
-
- Repeat steps 2 and 3 for
num2to get integerscandd.
- Repeat steps 2 and 3 for
-
- Calculate the real part of the product:
real = a * c - b * d.
- Calculate the real part of the product:
-
- Calculate the imaginary part of the product:
imag = a * d + b * c.
- Calculate the imaginary part of the product:
-
- Construct the result string by concatenating the calculated real part,
+, the imaginary part, andi.
- Construct the result string by concatenating the calculated real part,
Walkthrough
The core idea is to leverage Java's java.util.regex package to robustly parse the input strings. The formula for multiplying two complex numbers (a + bi) and (c + di) is (ac - bd) + (ad + bc)i.
First, we define a regular expression pattern like (-?\d+)\+(-?\d+)i that captures two integer groups: the real part and the imaginary part. We compile this pattern and create a Matcher for each input string (num1 and num2). By calling matcher.find(), we can access the captured groups using matcher.group(1) for the real part and matcher.group(2) for the imaginary part. These captured strings are then parsed into integers (e.g., a, b, c, d). After obtaining the integer values, we apply the multiplication formula to compute the new real and imaginary parts. Finally, the resulting numbers are formatted back into the required string format "real+imaginaryi".
import java.util.regex.Matcher;import java.util.regex.Pattern; class Solution { public String complexNumberMultiply(String num1, String num2) { Pattern pattern = Pattern.compile("(-?\\d+)\\+(-?\\d+)i"); Matcher m1 = pattern.matcher(num1); m1.find(); int a = Integer.parseInt(m1.group(1)); int b = Integer.parseInt(m1.group(2)); Matcher m2 = pattern.matcher(num2); m2.find(); int c = Integer.parseInt(m2.group(1)); int d = Integer.parseInt(m2.group(2)); int realPart = a * c - b * d; int imagPart = a * d + b * c; return realPart + "+" + imagPart + "i"; }}Complexity
Time
O(1) - The length of the input strings is bounded by a small constant. The time taken by the regex engine, parsing, and arithmetic operations is therefore constant.
Space
O(1) - The space used for the pattern, matchers, and storing the parsed integers is constant regardless of the input values (within the given constraints).
Trade-offs
Pros
Very robust for parsing complex string formats.
The code can be quite readable if the regex pattern is understood.
Cons
Can be less performant than direct string manipulation due to the overhead of compiling the pattern and the regex engine's state machine.
Might be overkill for a simple and fixed format like this.
Solutions
Solution
class Solution { public String complexNumberMultiply ( String num1 , String num2 ) { String [] c1 = num1 . split ( "\\+|i" ); String [] c2 = num2 . split ( "\\+|i" ); int a = Integer . parseInt ( c1 [ 0 ]); int b = Integer . parseInt ( c1 [ 1 ]); int c = Integer . parseInt ( c2 [ 0 ]); int d = Integer . parseInt ( c2 [ 1 ]); return String . format ( "%d+%di" , a * c - b * d , a * d + c * b ); } }Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.