{x}
blog image

Complex Number Multiplication

A complex number can be represented as a string on the form "real+imaginaryi" where:

  • real is the real part and is an integer in the range [-100, 100].
  • imaginary is 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:

  • num1 and num2 are valid complex numbers.

Solution Explanation: Complex Number Multiplication

This problem involves multiplying two complex numbers represented as strings and returning the result as a string in the format "a+bi". The solution employs a straightforward approach: parsing the input strings, performing the multiplication, and formatting the output.

Approach:

  1. Parsing: The input strings are parsed to extract the real and imaginary parts of each complex number. This involves splitting the string at the '+' sign and parsing the resulting substrings into integers. Handling of potential negative signs is implicitly managed by the parsing functions.

  2. Multiplication: The complex numbers are multiplied using the formula for multiplying complex numbers: (a + bi) * (c + di) = (ac - bd) + (ad + bc)i

  3. Formatting Output: The real and imaginary parts of the resulting complex number are formatted into the required string format "a+bi".

Time Complexity Analysis:

The time complexity is dominated by the parsing and formatting steps. Parsing the strings involves a constant number of string operations (splitting and converting to integers) which takes constant time. Similarly, formatting the output requires a constant number of string operations. Therefore, the overall time complexity is O(1), constant time.

Space Complexity Analysis:

The space complexity is also O(1) because the amount of extra space used does not depend on the input size. A constant amount of space is needed to store the parsed integer values and the intermediate calculation results. The output string also has a fixed size, regardless of the input values.

Code Implementation Details (Python Example):

class Solution:
    def complexNumberMultiply(self, num1: str, num2: str) -> str:
        a1, b1 = map(int, num1[:-1].split("+"))  #parse num1, removing the 'i' at the end.
        a2, b2 = map(int, num2[:-1].split("+"))  #parse num2, removing the 'i' at the end.
        real_part = a1 * a2 - b1 * b2
        imag_part = a1 * b2 + a2 * b1
        return f"{real_part}+{imag_part}i"  #format the output string.
 

Other Languages:

The solutions in other languages (Java, C++, Go, TypeScript) follow the same basic algorithmic approach. They differ only in the specifics of string parsing and formatting functions provided by each language. The core mathematical calculation remains unchanged. The Java solution, for instance, uses more explicit parsing methods compared to the more concise Python approach. The C++ code uses sscanf for efficient parsing.

In summary, the solution to this problem is efficient in both time and space complexity, focusing on clear and concise code for the parsing, calculation, and formatting phases.