537. Complex Number Multiplication
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to multiply two complex numbers given in a specific string format: "real+imaginaryi". The core task is to parse these strings to extract their numerical real and imaginary parts, perform the standard mathematical multiplication of complex numbers, and then format the resulting complex number back into the specified string format.
Solution
Complex Number Multiplication in Python
class Solution:
def complexNumberMultiply(self, num1: str, num2: str) -> str:
# Parse the first complex number using string splitting and slicing.
# e.g., "1+1i" -> split('+') -> ["1", "1i"]
parts1 = num1.split('+')
a = int(parts1[0])
b = int(parts1[1][:-1]) # [:-1] removes the trailing 'i'
# Parse the second complex number similarly.
parts2 = num2.split('+')
c = int(parts2[0])
d = int(parts2[1][:-1])
# Apply the complex number multiplication formula:
# (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
real_res = a * c - b * d
imag_res = a * d + b * c
# Format the result back into the required string format using an f-string.
return f"{real_res}+{imag_res}i"
Complex Number Multiplication in Java
class Solution {
public String complexNumberMultiply(String num1, String num2) {
// Parse the first complex number to get its real (a) and imaginary (b) parts.
int plus_pos1 = num1.indexOf('+');
int a = Integer.parseInt(num1.substring(0, plus_pos1));
// The imaginary part string is between '+' and 'i'.
int b = Integer.parseInt(num1.substring(plus_pos1 + 1, num1.length() - 1));
// Parse the second complex number to get its real (c) and imaginary (d) parts.
int plus_pos2 = num2.indexOf('+');
int c = Integer.parseInt(num2.substring(0, plus_pos2));
int d = Integer.parseInt(num2.substring(plus_pos2 + 1, num2.length() - 1));
// Apply the complex number multiplication formula:
// (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
int real_res = a * c - b * d;
int imag_res = a * d + b * c;
// Format the result back into the required string format "real+imaginaryi".
return real_res + "+" + imag_res + "i";
}
}
Complex Number Multiplication in C++
class Solution {
public:
string complexNumberMultiply(string num1, string num2) {
// Helper lambda to parse the complex number string.
// It finds the '+' separator and extracts the real and imaginary parts.
auto parse = [](const string& s) -> pair<int, int> {
size_t plus_pos = s.find('+');
// Extract real part string (before '+') and convert to integer.
int real = stoi(s.substr(0, plus_pos));
// Extract imaginary part string (from after '+' to before 'i') and convert to integer.
int imag = stoi(s.substr(plus_pos + 1, s.length() - plus_pos - 2));
return {real, imag};
};
// Parse both complex numbers to get their real and imaginary parts.
auto [a, b] = parse(num1); // num1 represents a + bi
auto [c, d] = parse(num2); // num2 represents c + di
// Apply the complex number multiplication formula:
// (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
int real_res = a * c - b * d;
int imag_res = a * d + b * c;
// Format the result back into the required string format "real+imaginaryi".
return to_string(real_res) + "+" + to_string(imag_res) + "i";
}
};
Complex Number Multiplication in TypeScript
function complexNumberMultiply(num1: string, num2: string): string {
// Helper function to parse a complex number string into its real and imaginary parts.
const parseComplex = (num: string): [number, number] => {
const plusIndex = num.indexOf('+');
const real = parseInt(num.substring(0, plusIndex));
// Extract imaginary part string (from after '+' to before 'i') and convert to integer.
const imag = parseInt(num.substring(plusIndex + 1, num.length - 1));
return [real, imag];
};
// Parse both complex numbers to get their real and imaginary parts.
const [a, b] = parseComplex(num1); // num1 represents a + bi
const [c, d] = parseComplex(num2); // num2 represents c + di
// Apply the complex number multiplication formula:
// (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
const real_res = a * c - b * d;
const imag_res = a * d + b * c;
// Format the result back into the required string format using a template literal.
return `${real_res}+${imag_res}i`;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.