Complex Number Multiplication

Problem Id: 537 Difficulty: Medium


Intuition

Solution


class Solution(object):
    def split(self, s):
        a, b = s.split('+')
        b = b.replace('i', '')
        return int(a), int(b)

    def complexNumberMultiply(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        a1, a2 = self.split(a)
        b1, b2 = self.split(b)
        c1 = a1 * b1 - a2 * b2
        c2 = a1 * b2 + a2 * b1
        return "%d+%di" % (c1, c2)