Given two strings representing two complex numbers.


You need to return a string representing their multiplication. Note i2 = -1 according to the definition.


Example 1:

Input: "1+1i", "1+1i"
Output: "0+2i"
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.



Example 2:

Input: "1+-1i", "1+-1i"
Output: "0+-2i"
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.



Note:

The input strings will not have extra blank.
The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Mathematics

Starter code: class Solution:
    def complexNumberMultiply(self, a: str, b: str) -> str:
        

Solutions:
class Solution:

	def complexNumberMultiply(self, a, b):
		a = a.split('+')
		b = b.split('+')
		a[1] = a[1][:-1]
		b[1] = b[1][:-1]
		a = list(map(int, a))
		b = list(map(int, b))
		print((a, b))
		r = a[0] * b[0] - a[1] * b[1]
		i = a[1] * b[0] + a[0] * b[1]
		print((r, i))
		return '{0}+{1}i'.format(r, i)


class Solution:

	def complexNumberMultiply(self, a, b):
		a1 = b1 = a2 = b2 = 0
		a = a.split('+')
		b = b.split('+')
		a1 = int(a[0])
		a2 = int(b[0])
		b1 = int(a[1][:-1])
		b2 = int(b[1][:-1])
		a3 = a1 * a2 - b1 * b2
		b3 = a1 * b2 + b1 * a2
		return str(a3) + '+' + str(b3) + 'i'


class Solution:

	def complexNumberMultiply(self, a, b):
		(a_r, a_u) = self.parse_str(a)
		(b_r, b_u) = self.parse_str(b)
		res_r = a_r * b_r - a_u * b_u
		res_u = a_r * b_u + a_u * b_r
		return '{0}+{1}i'.format(res_r, res_u)

	def parse_str(self, str):
		strs = str.split('+')
		real = int(strs[0])
		unreal = int(strs[1][:-1])
		return (real, unreal)


class Solution:

	def complexNumberMultiply(self, a, b):
		(ae, af) = a.split('+')
		af = af[:-1]
		(ae, af) = (int(ae), int(af))
		(bg, bh) = b.split('+')
		bh = bh[:-1]
		(bg, bh) = (int(bg), int(bh))
		ae_bg = ae * bg
		ae_bh = ae * bh
		af_bg = af * bg
		af_bh = af * bh
		first_term = ae_bg + -1 * af_bh
		second_term = ae_bh + af_bg
		format_str = '{}+{}i'.format(str(first_term), str(second_term))
		return format_str


class Solution:

	def complexNumberMultiply(self, a, b):
		aR = self.getReal(a)
		bR = self.getReal(b)
		aI = self.getImg(a)
		bI = self.getImg(b)
		real = aR * bR - aI * bI
		img = aR * bI + aI * bR
		return str(real) + '+' + str(img) + 'i'

	def getReal(self, s):
		res = s[0]
		for char in s[1:]:
			if char == '+' or char == '-':
				break
			else:
				res += char
		return int(res)

	def getImg(self, s):
		res = ''
		pos = 0
		for i in range(1, len(s)):
			if s[i] == '+' or s[i] == '-':
				pos = i + 1
				break
		while s[pos] != 'i':
			res += s[pos]
			pos += 1
		return int(res)


class Solution:

	def complexNumberMultiply(self, a, b):
		indice1 = a.index('+')
		indice2 = b.index('+')
		real1 = int(a[0:indice1])
		unreal1 = int(a[indice1 + 1:-1])
		real2 = int(b[0:indice2])
		unreal2 = int(b[indice2 + 1:-1])
		return str(real1 * real2 - unreal1 * unreal2) + '+' + str(unreal1 * real2 + real1 * unreal2) + 'i'


class Solution:

	def complexNumberMultiply(self, a, b):

		def parse_complex(s):
			(real, image) = s.split('+')
			return (int(real), int(image[:-1]))
		(r_a, i_a) = parse_complex(a)
		(r_b, i_b) = parse_complex(b)

		def construct_complex(real, image):
			return str(real) + '+' + str(image) + 'i'
		return construct_complex(r_a * r_b - i_a * i_b, r_a * i_b + r_b * i_a)


class Solution:

	def complexNumberMultiply(self, a, b):
		a1 = int(a.split('+')[0])
		a2 = int(a.split('+')[1].replace('i', ''))
		b1 = int(b.split('+')[0])
		b2 = int(b.split('+')[1].replace('i', ''))
		m1 = a1 * b1 - a2 * b2
		m2 = a1 * b2 + a2 * b1
		return str(m1) + '+' + str(m2) + 'i'


class Solution:

	def complexNumberMultiply(self, a, b):
		ca = self.toTuple(a)
		cb = self.toTuple(b)
		return self.fromTuple(self.multiply(ca, cb))

	def toTuple(self, a):
		index = a.index('+')
		return (int(a[:index]), int(a[index + 1:-1]))

	def fromTuple(self, a):
		return '{}+{}i'.format(a[0], a[1])

	def multiply(self, a, b):
		return (a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1])


class Solution:

	def complexNumberMultiply(self, a, b):
		(re_a, im_a) = map(int, a.replace('i', '').split('+'))
		(re_b, im_b) = map(int, b.replace('i', '').split('+'))
		return str(re_a * re_b - im_a * im_b) + '+' + str(re_a * im_b + re_b * im_a) + 'i'


class Solution:

	def complexNumberMultiply(self, a, b):
		(A, B) = self.parse_complex(a)
		(C, D) = self.parse_complex(b)
		return '%d+%di' % (A * C - B * D, A * D + B * C)

	def parse_complex(self, content):
		[a, b] = content.split('+')
		return (int(a), int(b[:-1]))


class Solution:

	def complexNumberMultiply(self, a, b):
		a = a.split('+')
		b = b.split('+')
		a1 = int(a[0])
		a2 = int(a[-1][:-1])
		b1 = int(b[0])
		b2 = int(b[-1][:-1])
		print(a1)
		print(a2)
		c1 = a1 * b1 - a2 * b2
		c2 = a1 * b2 + a2 * b1
		return str(c1) + '+' + str(c2) + 'i'