Multiply Strings
Given two strings, which are eventually some numbers, we need to find a product of these strings and return the result as a string. This is a problem from leetcode at this link .
Solution: Naiive Solution
def string_to_num(strr): my_num = 0 for i in strr: my_num = (my_num*10)+int(i) return my_num def num_to_string(intt): if intt==0: return '0' strr = '' while(intt): strr = str(intt%10) + strr intt = intt//10 return strr class Solution: def multiply(self, num1: str, num2: str) -> str: num11 = string_to_num(num1) num22 = string_to_num(num2) return num_to_string(num11*num22)
The problem wants us to implement the multiplication that does not use the large integer storage. So technically the above problem does not achieve what it is supposed to. The way to tackle this is by storing the integer product of the first number with each digit of the second number and summing them at the end.