Two deep-sea telemetry beacons each transmit a single reading encoded as a short text string of the form a+bi, where a is the beacon's baseline offset and b is its phase coefficient (always written immediately before a trailing letter i). The mission console needs to know what reading a receiver would see if it combined the two beacons' signals by treating each reading as the value a + b*i and multiplying the two values together using the usual rule (a + b*i) * (c + d*i) = (a*c - b*d) + (a*d + b*c)*i. Given the two beacon readings, compute and report the combined reading in the exact same string format.
a+bi.c+di.Each reading is written by concatenating the ordinary decimal representation of its offset, a literal + character, the ordinary decimal representation of its coefficient, and the letter i. Integers are written the usual way (a leading - only when negative, never a leading + when positive, no extra digits), so the literal + separating the two parts appears exactly once, immediately after the offset, even when the coefficient itself is negative (for example, an offset of 3 and a coefficient of -2 is written as 3+-2i).
a+bi format described above, using a*c - b*d as the offset and a*d + b*c as the coefficient.a, b are the first reading's offset and coefficient and c, d are the second reading's.Example 1
Input
3+2i 1+4i
Expected
-5+14i
Explanation
The first beacon's reading is 3 + 2i and the second is 1 + 4i. Multiplying gives real part 3*1 - 2*4 = -5 and imaginary part 3*4 + 2*1 = 14, so the combined reading is -5+14i.
Example 2
Input
-2+5i 3+-4i
Expected
14+23i
Explanation
The first beacon's reading is -2 + 5i and the second is 3 + (-4)i (written as `3+-4i`). Multiplying gives real part (-2)*3 - 5*(-4) = -6 + 20 = 14 and imaginary part (-2)*(-4) + 5*3 = 8 + 15 = 23, so the combined reading is 14+23i.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →