Multiply Strings - Simple string operations and heuristics
Problem Statement:
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 <= 200
num1
andnum2
consist of digits only.- Both
num1
andnum2
do not contain any leading zero, except the number0
itself.
Solution:
We will use several heuristics for this problem:
- Given two string numbers to multiply, we can treat one input as the multiplicator and another as multiplicand. We go over the multiplicator digit by digit from left to right and get the product of each digit with the multiplicand using a function digitMul that we will create. Once we get the output of digitMul, we add zeroes at the end of the output. The number of zeroes is equal to the place value of the digit inside multiplicator.
- You must have guessed that if we simply add the outputs of the above operation, we can get the answer. So we create a function add to add two string numbers.
- Now we need to write the logic of add and digitMul. To write add and digitMul functions, we can do it simply like how we learnt in kindergarten.
class Solution {
public:
string add(string a, string b)
{
if (!a.length()) return b;
if (!b.length()) return a;
string res = "";
int carry = 0;
while(a.length() && b.length())
{
int cur = (a.back()-'0')+(b.back()-'0')+carry;
res += to_string(cur%10);
carry = cur/10;
a.pop_back();
b.pop_back();
}
while(a.length())
{
int cur = (a.back()-'0')+carry;
res += to_string(cur%10);
carry = cur/10;
a.pop_back();
}
while(b.length())
{
int cur = (b.back()-'0')+carry;
res += to_string(cur%10);
carry = cur/10;
b.pop_back();
}
if (carry) res += to_string(carry);
reverse(res.begin(),res.end());
return res;
}
string digitMul(int a, string num)
{
string res = "";
int carry = 0;
for (int i=num.size()-1; i>=0; i--)
{
int cur = a * (num[i]-'0') + carry;
res.push_back('0' + (cur%10));
carry = (cur/10);
}
if (carry) res.push_back('0'+carry);
reverse(res.begin(),res.end());
return res;
}
string multiply(string num1, string num2)
{
if (num1=="0" || num2=="0") return "0";
string result = "";
for (int i=0; i<num1.length(); i++)
{
string product = digitMul(num1[i]-'0', num2);
product += string(num1.length()-i-1, '0');
result = add(result, product);
}
return result;
}
};