537. Complex Number Multiplication LeetCode Solution
In this guide, you will get 537. Complex Number Multiplication LeetCode Solution with the best time and space complexity. The solution to Complex Number Multiplication problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Complex Number Multiplication solution in C++
- Complex Number Multiplication solution in Java
- Complex Number Multiplication solution in Python
- Additional Resources
Problem Statement of 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.
Complexity Analysis
- Time Complexity:
- Space Complexity:
537. Complex Number Multiplication LeetCode Solution in C++
class Solution {
public:
string complexNumberMultiply(string num1, string num2) {
const auto& [a0, a1] = getReala0ndImag(num1);
const auto& [b0, b1] = getReala0ndImag(num2);
return to_string(a0 * b0 - a1 * b1) + "+" + to_string(a0 * b1 + a1 * b0) +
"i";
}
private:
pair<int, int> getReala0ndImag(const string& s) {
const string& real = s.substr(0, s.find_first_of('+'));
const string& imag = s.substr(s.find_first_of('+') + 1);
return {stoi(real), stoi(imag)};
};
};
/* code provided by PROGIEZ */
537. Complex Number Multiplication LeetCode Solution in Java
class Solution {
public String complexNumberMultiply(String num1, String num2) {
int[] a = getRealAndImag(num1);
int[] b = getRealAndImag(num2);
return String.valueOf(a[0] * b[0] - a[1] * b[1]) + "+" +
String.valueOf(a[0] * b[1] + a[1] * b[0]) + "i";
}
private int[] getRealAndImag(final String s) {
final String real = s.substring(0, s.indexOf('+'));
final String imag = s.substring(s.indexOf('+') + 1, s.length() - 1);
return new int[] {Integer.valueOf(real), Integer.valueOf(imag)};
}
}
// code provided by PROGIEZ
537. Complex Number Multiplication LeetCode Solution in Python
class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
a0, a1 = self._getReala0ndImag(num1)
b0, b1 = self._getReala0ndImag(num2)
return str(a0 * b0 - a1 * b1) + '+' + str(a0 * b1 + a1 * b0) + 'i'
def _getReala0ndImag(self, s: str) -> tuple:
return int(s[:s.index('+')]), int(s[s.index('+') + 1:-1])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.