LeetCode has a Medium coding Problem in Its’ Algorithm Section “Multiply strings leetcode python”. Today We are going to solve this problem. LeetCode Link of the Problem is HERE
Given two non-negative integers
num2 represented as strings, return the product of
num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
1 <= num1.length, num2.length <= 200
num2consist of digits only.
num2do not contain any leading zero, except the number
Solution to Multiply strings leetcode python
Both numbers’ multiplication begins with the ones place digit (the right-most digit), thus we should begin our multiplication at index num2.size() – 1 and work our way down to index 0. Alternatively, we may iterate from index 0 to index num2.size() – 1 by reversing both inputs.
We’ll obtain a new intermediate result for each digit in num2 that we multiply by num1. Depending on the language, this intermediate result (currentResult) will be kept in a list, string, or StringBuilder. To compute each intermediate result, we’ll start by inserting the proper amount of zeros in the second number, based on the current digit’s position (i.e. if it is the hundreds place, we append 2 zeros). Then, as shown in the diagrams above, we’ll do the multiplication step. We will enter the lower place digits into the currentResult before the higher place numbers during this phase. Our result will be in reverse order since we are pushing the lower place digits first and always appending to the end, so after the multiplication and addition processes are completed, we will need to invert the answer before returning.
Complete Solution in Python
This Solution is not according to the above explaination. It is an easy Solution.
return str(int(num1) * int(num2))
Python-related posts Visit HERE
C++ related posts Visit HERE
Databases related posts Visit HERE
Data Structures related posts visit HERE
Algorithms related posts visit HERE
Data Science related posts visit HERE