537. Complex Number Multiplication

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

Example 1:

Input: "1+1i", "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: "1+-1i", "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.

Note:

  1. The input strings will not have extra blank.
  2. The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

Rust Solution

struct Solution;
use std::fmt;
use std::ops::Mul;

struct Complex {
    r: i32,
    i: i32,
}

impl Complex {
    fn new(r: i32, i: i32) -> Self {
        Complex { r, i }
    }
    fn from_string(s: String) -> Self {
        let p = s.find('+').unwrap();
        let n = s.len();
        let r = s[0..p].parse::<i32>().unwrap();
        let i = s[p + 1..n - 1].parse::<i32>().unwrap();
        Self::new(r, i)
    }
}

impl Mul for Complex {
    type Output = Complex;
    fn mul(self, rhs: Complex) -> Self::Output {
        Self::new(
            self.r * rhs.r - self.i * rhs.i,
            self.r * rhs.i + self.i * rhs.r,
        )
    }
}

impl fmt::Display for Complex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}+{}i", self.r, self.i)
    }
}

impl Solution {
    fn complex_number_multiply(a: String, b: String) -> String {
        (Complex::from_string(a) * Complex::from_string(b)).to_string()
    }
}

#[test]
fn test() {
    let a = "1+1i".to_string();
    let b = "1+1i".to_string();
    let res = "0+2i".to_string();
    assert_eq!(Solution::complex_number_multiply(a, b), res);
    let a = "1+-1i".to_string();
    let b = "1+-1i".to_string();
    let res = "0+-2i".to_string();
    assert_eq!(Solution::complex_number_multiply(a, b), res);
}

Having problems with this solution? Click here to submit an issue on github.