24. Multiply two strings
The problem can be found at the following link: Question Link
My Approach
I've implemented multiplication of two strings by simulating the process as we manually does, similar to how we do long multiplication by hand.
Reverse both input strings to start multiplication from the least significant digits.
Handle negative signs by checking the last characters of the strings and
poppingthem if necessary.Remove
trailingzeros from both strings.Initialize an output string of length
len1 + len2with '0'.Iterate through each digit of the second number and multiply it with each digit of the first number, adding the result to the corresponding position in the output string.
Handle
carryduringmultiplicationandaddition.Remove
trailing zerosfrom the output string.Add a
negativesign to the output string if either of the input strings was negative.Reverse the output string to obtain the correct result.
Explanation with Example
Suppose we want to multiply "123" and "45":
Reverse both strings:
321and54.No negative signs.
No trailing zeros.
Initialize the result as "00000".
Multiply and add:
on
3 * 5 = "51000"on
3 * 4 = "53100"on
2 * 5 = "53200"on
2 * 4 = "53010"on
1 * 5 = "53510"on
1 * 4 = "53550"
Handle carry if any (none in this case) so
out = "53550".Remove trailing zeros, leaving
"53550".No negative sign needed.
Reverse the result to get
"5535".
Time and Auxiliary Space Complexity
Time Complexity:
O(len1 * len2), wherelen1is the length of the first string andlen2is the length of the second string.Auxiliary Space Complexity:
O(len1 + len2), as we use an output string of lengthlen1 + len2.
Code (C++)
class Solution{
public:
string multiplyStrings(string s1, string s2) {
reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
bool isNeg1 = 0, isNeg2 = 0;
if(s1.back() == '-') { s1.pop_back(); isNeg1 = 1; }
if(s2.back() == '-') { s2.pop_back(); isNeg2 = 1; }
while(s1.back() == '0') s1.pop_back();
while(s2.back() == '0') s2.pop_back();
int len1 = s1.size(), len2 = s2.size();
string out(len1 + len2, '0');
for (int i = 0; i < len2; ++i) {
int carry = 0, j;
for (j = 0; j < len1; ++j) {
int mul = (s2[i] - '0') * (s1[j] - '0') + (out[i + j] - '0') + carry;
out[i + j] = (char)('0' + mul % 10);
carry = mul / 10;
}
while (carry) {
int mul = (out[i + j] - '0') + carry;
out[i + j] = (char)('0' + mul % 10);
carry = mul / 10;
++j;
}
}
while (out.back() == '0')
out.pop_back();
if(!out.size())
return "0";
if (isNeg1 ^ isNeg2)
out += '-';
reverse(out.begin(), out.end());
return out;
}
};Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated
Was this helpful?