Ctrl K

Multiply Complex Numbers in Strings

Medium
A complex number a + bi is written as a string "a+bi", where a and b are integers. The + is always there, even when b is negative: 3 − 2i is written as "3+-2i", and −4 as "-4+0i". Given two such strings first and second, multiply the numbers (remember that i² = −1) and return the product in the same format.
Example 1
first="2+3i", second="4+-1i" → '11+10i'

(2 + 3i)(4 − i) = 8 − 2i + 12i − 3i² = 8 + 3 + 10i = 11 + 10i.

Example 2
first="0+2i", second="0+5i" → '-10+0i'

2i · 5i = 10i² = −10 with a zero imaginary part, written as -10+0i.

Example 3
first="-3+0i", second="1+-7i" → '-3+21i'

−3 · (1 − 7i) = −3 + 21i.

Constraints
  • Both strings use the "a+bi" format
  • −100 ≤ a, b ≤ 100 in each number
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMultiplying Digit Strings MediumTotal of a Fraction Chain MediumSolve for x MediumRover on Repeat MediumSum of Binary Strings Easy
loading Python…
Input
first="2+3i", second="4+-1i"
Expected
'11+10i'

Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.