Complex Number Multiplication - Problem

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]
  • == -1

Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplication.

Input & Output

Example 1 — Basic Multiplication
$ Input: num1 = "1+1i", num2 = "1+1i"
Output: "0+2i"
💡 Note: Using formula (a+bi)(c+di) = (ac-bd) + (ad+bc)i: (1+1i)(1+1i) = (1×1 - 1×1) + (1×1 + 1×1)i = 0 + 2i
Example 2 — With Negative Components
$ Input: num1 = "1+1i", num2 = "1-1i"
Output: "2+0i"
💡 Note: (1+1i)(1-1i) = (1×1 - 1×(-1)) + (1×(-1) + 1×1)i = (1+1) + (-1+1)i = 2+0i
Example 3 — Both Parts Negative
$ Input: num1 = "1-1i", num2 = "1-1i"
Output: "0-2i"
💡 Note: (1-1i)(1-1i) = (1×1 - (-1)×(-1)) + (1×(-1) + (-1)×1)i = (1-1) + (-1-1)i = 0-2i

Constraints

  • num1 and num2 are valid complex numbers in the form "a+bi" or "a-bi"
  • a and b are integers in the range [-100, 100]
  • The output format should match the input format

Visualization

Tap to expand
Complex Number Multiplication INPUT Complex Numbers as Strings num1 = "1+1i" (real=1, imag=1) num2 = "1+1i" (real=1, imag=1) Complex Plane Re Im 1+1i ALGORITHM STEPS 1 Parse with Regex Pattern: (-?\d+)\+(-?\d+)i 2 Extract Components a=1, b=1, c=1, d=1 3 Apply Formula (a+bi)(c+di) real = ac - bd imag = ad + bc (i² = -1) 4 Calculate real = 1*1 - 1*1 = 0 imag = 1*1 + 1*1 = 2 FINAL RESULT Multiplication Result "0+2i" Breakdown: (1+1i) * (1+1i) = 1 + 1i + 1i + i² = 1 + 2i - 1 = 0+2i Result on Plane 0+2i OK - Verified! Key Insight: Regex pattern (-?\d+)\+(-?\d+)i efficiently extracts real and imaginary parts from string format. Complex multiplication uses: (a+bi)(c+di) = (ac-bd) + (ad+bc)i, where i² = -1. Time Complexity: O(n) for parsing, Space Complexity: O(1) for calculation. TutorialsPoint - Complex Number Multiplication | Regex-Based Parsing Approach
Asked in
Facebook 15 Google 12 Amazon 8
28.5K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen