-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMultiply Strings.cpp
41 lines (30 loc) · 1.08 KB
/
Multiply Strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
string multiply(string num1, string num2) {
int n = num1.length();
int m = num2.length();
string ans(n+m,'0');
for(int i = n-1; i>=0;i--){
for(int j = m-1; j >= 0; j--){
int x = (num1[i]-'0') * (num2[j]-'0');
x += (ans[i+j+1] - '0');
// cout << x<<" "<<num1[i]<<" "<<num2[j]<<"\n";
ans[i+j+1] = (x%10) + '0';
ans[i+j] += ((x/10));
}
}
for(int i = 0; i<n+m;i++){
if(ans[i] != '0') return ans.substr(i);
}
return "0";
}
};