Simple ten line Cpp Solution :)


#1

string Solution::multiply(string A, string B) {
int n = A.length(),m = B.length();
string res(n+m,‘0’);

for(int i=n-1;i>=0;i--){
    for(int j=m-1;j>=0;j--){
        int num = (A[i] - '0') * (B[j] - '0') + res[i+j+1] - '0';
        res[i+j+1] = num%10 + '0';
        res[i+j] += num/10;
    }
}
for(int i=0;i<res.length();i++) if(res[i] != '0') return res.substr(i);
return "0";

}


#2

What is the logic behind i+j+1 and i+j ?


#3

Great work!!
But it does not work for negative numbers, just a few more lines and some modifications will do the job


#4

Think of how you learned to multiply in elementary school and how you shifted to the left (i) when you moved to the next number.