Last updated on January 5th, 2025 at 11:56 pm
Here, we see a Complex Number Multiplication LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Math, String
Companies
Amazon
Level of Question
Medium
Complex Number Multiplication LeetCode Solution
Table of Contents
1. Problem Statement
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.
2. Coding Pattern Used in Solution
The coding pattern used in the provided code is the String Parsing and Arithmetic Operations pattern. This pattern involves parsing strings to extract numerical components, performing arithmetic operations on them, and then formatting the result back into a string.
3. Code Implementation in Different Languages
3.1 Complex Number Multiplication 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) ; } };
3.2 Complex Number Multiplication 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.3 Complex Number Multiplication 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`; };
3.4 Complex Number Multiplication 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
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(n) | O(n) |
Java | O(n) | O(n) |
JavaScript | O(n) | O(n) |
Python | O(n) | O(n) |