Given two strings representing two complex numbers.
You need to return a string representing their multiplication. Note i^2= -1 according to the definition.
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.
class Solution {
public String complexNumberMultiply(String a, String b) {
int i1 = a.indexOf('+');
int i2 = b.indexOf('+');
int r1 = Integer.parseInt(a.substring(0, i1));
int r2 = Integer.parseInt(b.substring(0, i2));
int c1 = Integer.parseInt(a.substring(i1 + 1, a.length() - 1));
int c2 = Integer.parseInt(b.substring(i2 + 1, b.length() - 1));
return String.valueOf(r1 * r2 - c1 * c2) + "+" + String.valueOf(r1 * c2 + r2 * c1) + "i";
}
}