|
| 1 | +--- |
| 2 | +id: complex-number-multiplication |
| 3 | +title: Complex Number Multiplication |
| 4 | +sidebar_label: 0537 - Complex Number Multiplication |
| 5 | +tags: |
| 6 | + - Math |
| 7 | + - String |
| 8 | + - Simulation |
| 9 | +description: "This is a solution to the Complex Number Multiplication problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +A complex number can be represented as a string on the form `"real+imaginaryi"` where: |
| 15 | + |
| 16 | +- `real` is the real part and is an integer in the range `[-100, 100]`. |
| 17 | +- `imaginary` is the imaginary part and is an integer in the range `[-100, 100]`. |
| 18 | +- $i^2 == -1$. |
| 19 | + |
| 20 | +Given two complex numbers `num1` and `num2` as strings, return a string of the complex number that represents their multiplications. |
| 21 | + |
| 22 | + |
| 23 | +### Examples |
| 24 | + |
| 25 | +**Example 1:** |
| 26 | + |
| 27 | + |
| 28 | +``` |
| 29 | +Input: num1 = "1+1i", num2 = "1+1i" |
| 30 | +Output: "0+2i" |
| 31 | +Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i. |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +``` |
| 37 | +Input: num1 = "1+-1i", num2 = "1+-1i" |
| 38 | +Output: "0+-2i" |
| 39 | +Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i. |
| 40 | +``` |
| 41 | + |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- `num1` and `num2` are valid complex numbers. |
| 45 | + |
| 46 | +## Solution for Complex Number Multiplication |
| 47 | + |
| 48 | +### Approach 1: |
| 49 | +#### Algorithm |
| 50 | + |
| 51 | +Multiplication of two complex numbers can be done as: |
| 52 | + |
| 53 | +$(a+ib)×(x+iy)=ax+i^2by+i(bx+ay)=ax−by+i(bx+ay)$ |
| 54 | + |
| 55 | +We simply split up the real and the imaginary parts of the given complex strings based on the '+' and the 'i' symbols. We store the real parts of the two strings a and b as x[0] and y[0] respectively and the imaginary parts as x[1] and y[1] respectively. Then, we multiply the real and the imaginary parts as required after converting the extracted parts into integers. Then, we again form the return string in the required format and return the result. |
| 56 | + |
| 57 | + |
| 58 | +## Code in Different Languages |
| 59 | + |
| 60 | +<Tabs> |
| 61 | +<TabItem value="cpp" label="C++"> |
| 62 | + <SolutionAuthor name="@Shreyash3087"/> |
| 63 | + |
| 64 | +```cpp |
| 65 | +#include <iostream> |
| 66 | +#include <string> |
| 67 | +#include <sstream> |
| 68 | + |
| 69 | +class Solution { |
| 70 | +public: |
| 71 | + std::string complexNumberMultiply(std::string num1, std::string num2) { |
| 72 | + auto parseComplex = [](const std::string& s) -> std::pair<int, int> { |
| 73 | + std::stringstream ss(s); |
| 74 | + std::string realPart, imagPart; |
| 75 | + getline(ss, realPart, '+'); |
| 76 | + getline(ss, imagPart, 'i'); |
| 77 | + return {std::stoi(realPart), std::stoi(imagPart)}; |
| 78 | + }; |
| 79 | + |
| 80 | + auto [a_real, a_img] = parseComplex(num1); |
| 81 | + auto [b_real, b_img] = parseComplex(num2); |
| 82 | + |
| 83 | + int real = a_real * b_real - a_img * b_img; |
| 84 | + int img = a_real * b_img + a_img * b_real; |
| 85 | + |
| 86 | + return std::to_string(real) + "+" + std::to_string(img) + "i"; |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | + |
| 91 | +``` |
| 92 | +</TabItem> |
| 93 | +<TabItem value="java" label="Java"> |
| 94 | + <SolutionAuthor name="@Shreyash3087"/> |
| 95 | + |
| 96 | +```java |
| 97 | + |
| 98 | +public class Solution { |
| 99 | + |
| 100 | + public String complexNumberMultiply(String a, String b) { |
| 101 | + String x[] = a.split("\\+|i"); |
| 102 | + String y[] = b.split("\\+|i"); |
| 103 | + int a_real = Integer.parseInt(x[0]); |
| 104 | + int a_img = Integer.parseInt(x[1]); |
| 105 | + int b_real = Integer.parseInt(y[0]); |
| 106 | + int b_img = Integer.parseInt(y[1]); |
| 107 | + return (a_real * b_real - a_img * b_img) + "+" + (a_real * b_img + a_img * b_real) + "i"; |
| 108 | + |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +``` |
| 113 | + |
| 114 | +</TabItem> |
| 115 | +<TabItem value="python" label="Python"> |
| 116 | + <SolutionAuthor name="@Shreyash3087"/> |
| 117 | + |
| 118 | +```python |
| 119 | +class Solution: |
| 120 | + def complexNumberMultiply(self, a: str, b: str) -> str: |
| 121 | + def parse_complex(s: str): |
| 122 | + real, imag = s.split('+') |
| 123 | + imag = imag[:-1] # remove the trailing 'i' |
| 124 | + return int(real), int(imag) |
| 125 | + |
| 126 | + a_real, a_img = parse_complex(a) |
| 127 | + b_real, b_img = parse_complex(b) |
| 128 | + |
| 129 | + real = a_real * b_real - a_img * b_img |
| 130 | + img = a_real * b_img + a_img * b_real |
| 131 | + |
| 132 | + return f"{real}+{img}i" |
| 133 | +``` |
| 134 | +</TabItem> |
| 135 | +</Tabs> |
| 136 | + |
| 137 | +## Complexity Analysis |
| 138 | + |
| 139 | +### Time Complexity: $O(1)$ |
| 140 | + |
| 141 | +> **Reason**: Here splitting takes constant time as length of the string is very small `(<20)`. |
| 142 | +
|
| 143 | +### Space Complexity: $O(1)$ |
| 144 | + |
| 145 | +> **Reason**: Constant extra space is used. |
| 146 | +
|
| 147 | +## References |
| 148 | + |
| 149 | +- **LeetCode Problem**: [Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/description/) |
| 150 | + |
| 151 | +- **Solution Link**: [Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/solutions/) |
0 commit comments