30 Days of Code [Day 9]
QUESTION : 43. Multiply Strings
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define fo(i,s,n) for(int i=s;i<n;++i)
#define of(i,s,n) for(int i=s-1;i>=n;--i)
#define fv(V) for( auto &it : V )
class Solution {
public:
string multiply(string num1, string num2) {
if(num1 == "0" || num2 == "0") return "0";
int n = num1.size();
int m = num2.size();
vi v(n+m, 0);
int pw = 0;
of(i,n,0){
int carry = 0;
int k = v.size()-1-pw;
of(j,m,0){
int s = (num1[i] - '0')*(num2[j] - '0') + carry + v[k];
carry = s/10;
v[k] = s%10;
k--;
}
if(carry > 0) v[k] += carry;
pw ++;
}
string res = "";
bool f = false;
fv(v){
if(!it && !f) continue;
else{
res += to_string(it);
f = true;
}
}
return res;
}
};