Leetcode 537. Complex Number Multiplication
Problem Explanation
In this problem, we're given two complex numbers as input strings in the form of "a+bi" where 'a' and 'b' are the real and imaginary parts of the complex number and 'i' is the imaginary unit. Our task is to multiply these two complex numbers and return the result as a string in the same format ("a+bi").
The multiplication of two complex numbers is performed as follows:
(A + Bi) * (C + Di) = (A*C - B*D) + (A*D + B*C)i
Where, A
and C
are real parts and B
and D
are imaginary parts.
For example, let's consider the multiplication of two complex numbers (1 + 1i)
& (1 + 1i)
.
The multiplication would be = (1*1 - 1*1) + (1*1 + 1*1)i
= 0 + 2i
Approach
We first extract the real and imaginary parts from the given string representations of the complex numbers using the in-built find_first_of
, substr
and stoi
functions.
Next, we compute the resulting real and imaginary parts of the product of the complex numbers by using the formula mentioned above.
Finally, we convert the resulting real and imaginary parts into an output string in the format of "a+bi" and return it.
Python Solution
1 2python 3class Solution: 4 def complexNumberMultiply(self, a: str, b: str) -> str: 5 a1, a2 = map(int, a[:-1].split('+')) 6 b1, b2 = map(int, b[:-1].split('+')) 7 return str(a1 * b1 - a2 * b2) + '+' + str(a1 * b2 + a2 * b1) + 'i'
Java Solution
1 2java 3class Solution { 4 public String complexNumberMultiply(String a, String b) { 5 int[] coefs1 = Stream.of(a.split("\\+|i")).mapToInt(Integer::parseInt).toArray(), 6 coefs2 = Stream.of(b.split("\\+|i")).mapToInt(Integer::parseInt).toArray(); 7 return (coefs1[0] * coefs2[0] - coefs1[1] * coefs2[1]) + "+" + 8 (coefs1[0] * coefs2[1] + coefs1[1] * coefs2[0]) + "i"; 9 } 10}
JavaScript Solution
1 2javascript 3var complexNumberMultiply = function(a, b) { 4 let aSplit = a.slice(0, a.length - 1).split('+').map(Number); 5 let bSplit = b.slice(0, b.length - 1).split('+').map(Number); 6 let real = aSplit[0] * bSplit[0] - aSplit[1] * bSplit[1]; 7 let imaginary = aSplit[0] * bSplit[1] + aSplit[1] * bSplit[0]; 8 return `${real}+${imaginary}i`; 9};
C++ Solution
1
2c++
3class Solution {
4public:
5 string complexNumberMultiply(string a, string b) {
6 pair<int, int> coeffs1 = parse(a), coeffs2 = parse(b);
7 int real = coeffs1.first*coeffs2.first - coeffs1.second*coeffs2.second;
8 int imag = coeffs1.first*coeffs2.second + coeffs1.second*coeffs2.first;
9 return to_string(real) + "+" + to_string(imag) + "i";
10 }
11private:
12 pair<int, int> parse(const string& s) {
13 int plus = find(s.begin(), s.end(), '+') - s.begin();
14 int i = find(s.begin(), s.end(), 'i') - s.begin();
15 int real = stoi(s.substr(0, plus));
16 int imag = stoi(s.substr(plus + 1, i - plus));
17 return {real, imag};
18 }
19};
C# Solution
1 2csharp 3public class Solution { 4 public string ComplexNumberMultiply(string a, string b) { 5 var aSplit = a.Substring(0,a.Length-1).Split('+').Select(int.Parse).ToArray(); 6 var bSplit = b.Substring(0,b.Length-1).Split('+').Select(int.Parse).ToArray(); 7 int real = aSplit[0]*bSplit[0] - aSplit[1]*bSplit[1]; 8 int imag = aSplit[0]*bSplit[1] + aSplit[1]*bSplit[0]; 9 return $"{real}+{imag}i"; 10 } 11}
These solutions are dividing both the string inputs into real and imaginary parts, then applying the given formula to get the real and imaginary parts of the product. Finally, these parts are being converted into a string and returned as the answer.Each solution uses similar logic with language-specific methods:
-
Python utilizes the map() function to apply the int() constructor to each element in the split strings, converting them to integers. The resulting real and imaginary parts are then computed and concatenated into a final output string.
-
The Java solution also begins by splitting the input strings, using Stream and mapToInt() to parse the integers. The product of the complex numbers is computed and returned as a string.
-
JavaScript uses the slice() and split() methods to extract numerical components from the strings, then computes the product according to the formula and outputs as a string.
-
The C++ solution uses the find() function to locate the '+' and 'i' in the string and then utilizes substr() and stoi() to extract and convert the real and imaginary parts. The product is calculated via the formula and returned as a string.
-
Finally, the C# solution uses Substring() and Split() functions to extract integers from strings. After computing the product, it constructs a string using the $ syntax for string interpolation.
All the given solutions have a time complexity of O(1) (constant time) as the operations performed do not depend on the input size. These solutions can accurately compute the product of two complex numbers represented as strings in the form "a+bi" and exhibit how various programming languages can tackle the same problem with their unique toolkits and syntax.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.