Algorithm


Problem Name: 537. 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.

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public String complexNumberMultiply(String num1, String num2) {
    ComplexNumber result = ComplexNumber.buildComplexNumber(num1).multiply(ComplexNumber.buildComplexNumber(num2));
    return result.toString();
  }
  
  
  private static class ComplexNumber {
    
    private final int real;
    private final int imaginary;
    
    private ComplexNumber(int real, int imaginary) {
      this.real = real;
      this.imaginary = imaginary;
    }
    
    public static ComplexNumber buildComplexNumber(String s) {
      int[] idx = {0};
      int realPart = parseRealPart(s, idx);
      int imaginaryPart = parseImaginaryPart(s, idx);
      return new ComplexNumber(realPart, imaginaryPart);
    }
    
    public ComplexNumber multiply(ComplexNumber anotherNumber) {
      int realPart = this.real * anotherNumber.real + (this.imaginary * anotherNumber.imaginary * -1);
      int imaginaryPart = this.real * anotherNumber.imaginary + this.imaginary * anotherNumber.real;
      return new ComplexNumber(realPart, imaginaryPart);
    }
    
    public String toString() {
      return this.real + "+" + this.imaginary + "i";
    }
    
    private static int parseRealPart(String s, int[] idx) {
      int realSign = 1;
      if (s.charAt(idx[0]) == '+' || s.charAt(idx[0]) == '-') {
        realSign = s.charAt(idx[0]) == '-' ? -1 : 1;
        idx[0]++;
      }
      int realNum = 0;
      while (idx[0]  <  s.length() && Character.isDigit(s.charAt(idx[0]))) {
        realNum = realNum * 10 + Character.getNumericValue(s.charAt(idx[0]++));
      }
      realNum *= realSign;
      idx[0]++;
      return realNum;
    }
    
    private static int parseImaginaryPart(String s, int[] idx) {
      int imaginarySign = 1;
      if (idx[0]  <  s.length() && (s.charAt(idx[0]) == '+' || s.charAt(idx[0]) == '-')) {
        imaginarySign = s.charAt(idx[0]) == '-' ? -1 : 1;
        idx[0]++;
      } 
      int imaginaryNum = 0;
      while (idx[0]  <  s.length() && s.charAt(idx[0]) != 'i') {
        imaginaryNum = imaginaryNum * 10 + Character.getNumericValue(s.charAt(idx[0]++));
      }
      return imaginarySign * imaginaryNum;
    }
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num1 = "1+1i", num2 = "1+1i"

Output

x
+
cmd
"0+2i"

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def complexNumberMultiply(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        re, im = 0, 0
        re_a, im_a = list(map(int,a[:-1].split("+")))
        re_b, im_b = list(map(int,b[:-1].split("+")))
        re += re_a * re_b - im_a * im_b
        im += re_a * im_b + re_b *im_a
        return str(re)+"+"+str(im)+"i"
Copy The Code & Try With Live Editor

Input

x
+
cmd
num1 = "1+1i", num2 = "1+1i"

Output

x
+
cmd
"0+2i"

#3 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0537_ComplexNumberMultiplication
    {
        public string ComplexNumberMultiply(string a, string b)
        {
            var split1 = a.Split(new char[] { '+', 'i' }, StringSplitOptions.RemoveEmptyEntries);
            var split2 = b.Split(new char[] { '+', 'i' }, StringSplitOptions.RemoveEmptyEntries);

            int a1 = int.Parse(split1[0]), b1 = int.Parse(split1[1]);
            int a2 = int.Parse(split2[0]), b2 = int.Parse(split2[1]);

            return $"{a1 * a2 - b1 * b2}+{a1 * b2 + a2 * b1}i";
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num1 = "1+-1i", num2 = "1+-1i"

Output

x
+
cmd
"0+-2i"
Advertisements

Demonstration


Previous
#535 Leetcode Encode and Decode TinyURL Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#538 Leetcode Convert BST to Greater Tree Solution in C, C++, Java, JavaScript, Python, C# Leetcode