Today we will be going over another LeetCode problem: "Multiplying Strings." This problem will have a solution similar to that of "Add Two Numbers." As always, the source code will be at the end of the post. Without further adieu, lets get right into it!
Multiply Strings
This medium difficulty problem asks us to multiply two strings:
The prompt also tells us that the lengths of the given strings can go up to 109 characters, the strings contains only digits (so we do not need to worry about numbers in word form), the strings do not contain leading zeros (like 0100 instead of 100), and we cannot libraries that can do the conversions for us.
Solution
At the beginning, I had an idea to use base 10 and the digits provided in the string to essentially translate the string version of the number into an integer. So if one of the strings is "2101", we can use the digits provided to make the follow conversion: "2101" -> 2000 + 100 + 1 = 2101 (int). The issue with this solution is that the strings may have up to 109 digits, which means we cannot store it in the primitive C++ integer. So this solution is a no go.A work around for this is similar to that of "Add Two Numbers." I created a vector representation of the number within the string and used the vectors to conduct the multiplication. In the multiplication, I had to consider the carry just like in "Add Two Numbers" (which I inadvertently called "extra" in the source code). I also ported over the addition code from my "Add Two Numbers" solution, as a part of long multiplication is the addition at the end:
When I was testing my code before submitting, I ran into a bug where my answer will report 1 leading zero:
The bug is located within my multiply_vector helper method, where I was pushing back the "remainder" and "extra" in my "sum" vector before completing the iteration of the outer loop. What I ended up doing was add some code to remove the leading zero when converting the result vector into a string. This ended up causing me to fail a test of 0 * 0, as the result will be zero and it will be filtered out when processing the result string.
This bug however made me realize that I can go through some multiplications rather quickly. One case would be if one of the given numbers is 0. If the number is 0, then I will return a string "0" as 0 times anything is 0. Another case will be if one of the given numbers is 1. If one of the numbers is 1, I return the other number given because 1 times x is x.
Results
The run time for this problem is significantly slower than the other problems I have done. One reason would probably be the double loops I use to conduct the multiplication. Another thing would be the conversion of the strings to vectors before doing the multiplication, as strings could go up to 109 characters as mentioned. What I could have done was use the given strings themselves for the multiplication rather than converting them to vectors.
Conclusion
Overall, this was a pretty interesting problem to solve. It was similar to "Add Two Numbers" in the sense that it involves converting some data into a form that is usable for processing and calculations. Thank you all for reading and I will see you next time!
Link to source code: https://github.com/EricFlorin/Leetcode-Solutions/blob/master/Multiply%20Strings.cpp