Complex Number Multiplication LeetCode Solution

Last updated on July 18th, 2024 at 10:14 pm

Here, We see Complex Number Multiplication LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Math, String

Companies

Amazon

Level of Question

Medium

Complex Number Multiplication LeetCode Solution

Complex Number Multiplication LeetCode Solution

Problem Statement

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.

1. Complex Number Multiplication LeetCode Solution C++

class Solution {
public:
    string complexNumberMultiply(string num1, string num2) {
        string res = "" ;
        int a1 = real(num1) ;
        int a2 = real(num2) ;
        int b1 = complex(num1) ;
        int b2 = complex(num2) ;
        res = res + to_string(a1*a2-b1*b2)+'+'+to_string(a1*b2+a2*b1)+'i' ;
        return res ;        
    }

    int real(string str){
        int i = 0 ;
        string real = "" ;
        while(str[i]!='+'){
            real += str[i] ;
            i++ ;
        }
        return stoi(real) ;
    }
    
    int complex(string str){
        int i = 0 ;
        string complex = "" ;
        while(str[i]!='+'){
            i++ ;
        }
        i++ ;
        while(str[i]!='i'){
            complex += str[i] ;
            i++ ;
        }
        return stoi(complex) ;
    }
};

2. Complex Number Multiplication Solution Java

class Solution {
    public String complexNumberMultiply(String num1, String num2) {
        String[] a = num1.split("\\+|i");
        String[] b = num2.split("\\+|i");
        int ar = Integer.parseInt(a[0]), ai = Integer.parseInt(a[1]);
        int br = Integer.parseInt(b[0]), bi = Integer.parseInt(b[1]);
        return String.format("%d+%di",ar*br -ai*bi, ar*bi + ai*br);        
    }
}

3. Complex Number Multiplication Solution JavaScript

var complexNumberMultiply = function(num1, num2) {
	var a = parseInt(num1.split("+")[0]);
	var b = parseInt(num1.split("+")[1].replace("i", ""));
	var c = parseInt(num2.split("+")[0]);
	var d = parseInt(num2.split("+")[1].replace("i", ""));
	return `${a * c - b * d}+${a * d + b * c}i`;    
};

4. Complex Number Multiplication Solution Python

class Solution(object):
    def complexNumberMultiply(self, num1, num2):
        a, b = int(num1[:num1.index("+")]), int(num1[num1.index("+")+1:-1])
        c, d = int(num2[:num2.index("+")]), int(num2[num2.index("+")+1:-1])
        real_part = a * c - b * d
        imag_part = a * d + b * c
        result = str(real_part) + "+" + str(imag_part) + "i"
        return result
Scroll to Top