537. Complex Number Multiplication ¶ Time: Space: C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15class Solution { public: string complexNumberMultiply(string a, string b) { const auto& [A, B] = getRealAndImag(a); const auto& [C, D] = getRealAndImag(b); return to_string(A * C - B * D) + "+" + to_string(A * D + B * C) + "i"; } private: pair<int, int> getRealAndImag(const string& s) { const string& real = s.substr(0, s.find_first_of('+')); const string& imag = s.substr(s.find_first_of('+') + 1); return {stoi(real), stoi(imag)}; }; }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public String complexNumberMultiply(String a, String b) { int[] A = getRealAndImag(a); int[] B = getRealAndImag(b); return String.valueOf(A[0] * B[0] - A[1] * B[1]) + "+" + String.valueOf(A[0] * B[1] + A[1] * B[0]) + "i"; } private int[] getRealAndImag(final String s) { final String real = s.substring(0, s.indexOf('+')); final String imag = s.substring(s.indexOf('+') + 1, s.length() - 1); return new int[] {Integer.valueOf(real), Integer.valueOf(imag)}; } } 1 2 3 4 5 6 7 8 9class Solution: def complexNumberMultiply(self, a: str, b: str) -> str: def getRealAndImag(s: str) -> tuple: return int(s[:s.index('+')]), int(s[s.index('+') + 1:-1]) A, B = getRealAndImag(a) C, D = getRealAndImag(b) return str(A * C - B * D) + '+' + str(A * D + B * C) + 'i'