Posted on

Description

Submission

class Solution {
    pair<int, int> complex2pair(const string& num) {
        auto it = find(num.begin(), num.end(), '+');
        int real = stoi(num.substr(0, it - num.begin()));
        int imaginary = stoi(num.substr(it - num.begin() + 1, num.end() - it - 2));
        return {real, imaginary};
    }

    string pair2complex(pair<int, int> p) {
        return to_string(p.first) + "+" + to_string(p.second) + "i";
    }
public:
    string complexNumberMultiply(string num1, string num2) {
        auto p1 = complex2pair(num1);
        auto p2 = complex2pair(num2);

        pair<int, int> p = {p1.first * p2.first - p1.second * p2.second, p1.first * p2.second + p1.second * p2.first};
        
        return pair2complex(p);
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *