Given two numbers represented as strings, return multiplication of the numbers as a string.
Difficulty: Medium
public class Solution {
public String multiply(String num1, String num2) {
int[] digits = new int[num1.length() + num2.length()];
String ans = "";
for(int i = 0; i < num1.length(); i++){
for(int j = 0; j < num2.length(); j++){
digits[i+j] += ((num1.charAt(num1.length() - 1 - i) - '0') * (num2.charAt(num2.length() - 1 - j) - '0'));
}
}
for(int i = 0; i < digits.length; i++){
ans = Integer.toString(digits[i]%10) + ans;
if(i < digits.length - 1){
digits[i + 1] += digits[i]/10;
}
}
int i = 0;
for(i = 0; i < ans.length(); i++){
if(ans.charAt(i) != '0'){
break;
}
}
if(ans.substring(i).length() == 0) return "0";
return ans.substring(i);
}
}