class Solution {
/** (a + bj)(c + dj) = (ac - bd) + (ad + bc) j */
public String[] parseComplex(String num) {
String real = "", imag = "";
boolean imagFlag = false;
char[] charArray = num.toCharArray();
for (char ch: charArray) {
if (ch == 'i') break;
if (ch == '+') imagFlag = true;
else if (imagFlag == false) real += String.valueOf(ch);
else imag += String.valueOf(ch);
}
return new String[]{real, imag};
}
public String complexNumberMultiply(String num1, String num2) {
String[] realImag1 = parseComplex(num1), realImag2 = parseComplex(num2);
int real1 = Integer.parseInt(realImag1[0]), imag1 = Integer.parseInt(realImag1[1]);
int real2 = Integer.parseInt(realImag2[0]), imag2 = Integer.parseInt(realImag2[1]);
return String.valueOf(real1 * real2 - imag1 * imag2) + "+" + String.valueOf(real1 * imag2 + imag1 * real2) + "i";
}
}