Problem

A complex number can be represented as a string on the form "**real** +**imaginary** i" 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.

Examples

Example 1

1
2
3
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

1
2
3
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

Method 1 – Direct Parsing and Multiplication

Intuition

A complex number can be represented as (a + bi). Multiplying two complex numbers (a + bi) and (c + di) gives (ac - bd) + (ad + bc)i. We parse the input strings to extract real and imaginary parts, then apply the formula.

Approach

  1. Parse each input string to extract the real and imaginary parts as integers.
  2. Use the formula for multiplication: (a + bi) * (c + di) = (ac - bd) + (ad + bc)i.
  3. Format the result as a string in the form “x+yi”.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
public:
    std::string complexNumberMultiply(std::string num1, std::string num2) {
        auto parse = [](const std::string& s) {
            int plus = s.find('+');
            int a = std::stoi(s.substr(0, plus));
            int b = std::stoi(s.substr(plus + 1, s.size() - plus - 2));
            return std::make_pair(a, b);
        };
        auto [a, b] = parse(num1);
        auto [c, d] = parse(num2);
        int real = a * c - b * d;
        int imag = a * d + b * c;
        return std::to_string(real) + "+" + std::to_string(imag) + "i";
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func ComplexNumberMultiply(num1 string, num2 string) string {
    parse := func(s string) (int, int) {
        var a, b int
        fmt.Sscanf(s, "%d+%di", &a, &b)
        return a, b
    }
    a, b := parse(num1)
    c, d := parse(num2)
    real := a*c - b*d
    imag := a*d + b*c
    return fmt.Sprintf("%d+%di", real, imag)
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public String complexNumberMultiply(String num1, String num2) {
        int[] n1 = parse(num1), n2 = parse(num2);
        int real = n1[0] * n2[0] - n1[1] * n2[1];
        int imag = n1[0] * n2[1] + n1[1] * n2[0];
        return real + "+" + imag + "i";
    }
    private int[] parse(String s) {
        String[] parts = s.split("\\+");
        int a = Integer.parseInt(parts[0]);
        int b = Integer.parseInt(parts[1].substring(0, parts[1].length() - 1));
        return new int[]{a, b};
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    fun complexNumberMultiply(num1: String, num2: String): String {
        fun parse(s: String): Pair<Int, Int> {
            val (a, b) = s.split("+")
            return Pair(a.toInt(), b.dropLast(1).toInt())
        }
        val (a, b) = parse(num1)
        val (c, d) = parse(num2)
        val real = a * c - b * d
        val imag = a * d + b * c
        return "${real}+${imag}i"
    }
}
1
2
3
4
5
6
7
8
9
def complex_number_multiply(num1: str, num2: str) -> str:
    def parse(s: str) -> tuple[int, int]:
        a, b = s[:-1].split('+')
        return int(a), int(b)
    a, b = parse(num1)
    c, d = parse(num2)
    real = a * c - b * d
    imag = a * d + b * c
    return f"{real}+{imag}i"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl Solution {
    pub fn complex_number_multiply(num1: String, num2: String) -> String {
        fn parse(s: &str) -> (i32, i32) {
            let parts: Vec<&str> = s[..s.len()-1].split('+').collect();
            (parts[0].parse().unwrap(), parts[1].parse().unwrap())
        }
        let (a, b) = parse(&num1);
        let (c, d) = parse(&num2);
        let real = a * c - b * d;
        let imag = a * d + b * c;
        format!("{}+{}i", real, imag)
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    complexNumberMultiply(num1: string, num2: string): string {
        function parse(s: string): [number, number] {
            const [a, b] = s.slice(0, -1).split('+');
            return [parseInt(a), parseInt(b)];
        }
        const [a, b] = parse(num1);
        const [c, d] = parse(num2);
        const real = a * c - b * d;
        const imag = a * d + b * c;
        return `${real}+${imag}i`;
    }
}

Complexity

  • ⏰ Time complexity: O(1)
  • 🧺 Space complexity: O(1)