Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"
Constraints:
1 <= num1.length, num2.length <= 200num1 and num2 consist of digits only.num1 and num2 do not contain any leading zero, except the number 0 itself.https://www.youtube.com/watch?v=5NdhK3tZViQ
class Solution {
public:
string multiply(string num1, string num2) {
if(num1=="0"||num2=="0")
return "0";
int l1 = num1.size();
int l2 = num2.size();
vector<int> res(l1+l2,0);
int i = l2-1;
int pf = 0;//power factor
while(i>=0)
{
int ival = num2[i] - '0';
int carry = 0;
int k = res.size() - 1 - pf;
int j = l1-1;
while(j>=0 || carry)
{
int jval;
if(j>=0)
jval = num1[j]-'0';
else
jval = 0;
int prod = ival*jval + carry + res[k];
res[k] = prod%10;
carry = prod/10;
j--;
k--;
}
pf++;
i--;
}
string ans = "";
for(int val:res)
{
ans+=to_string(val);
}
//Managing leading 0s
int in = 0;
int jn = ans.size();
for(;in<ans.size();in++)
{
if(ans[in]!='0')
break;
}
return ans.substr(in,jn-in+1);
}
};