Advertisement
Iam_Sandeep

MULTIPLY STRINGS

Sep 16th, 2021
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. https://leetcode.com/problems/multiply-strings/
  2. class Solution:
  3.     def multiply(self, num1: str, num2: str) -> str:
  4.         num1,num2=num1[::-1],num2[::-1]
  5.         m,n=len(num1),len(num2)
  6.         res=[0]*(m+n)
  7.         for i in range(m):
  8.             for j in range(n):
  9.                 digit=int(num1[i])*int(num2[j])
  10.                 res[i+j]+=digit
  11.                 rem=res[i+j]//10
  12.                 res[i+j]=res[i+j]%10
  13.                 res[i+j+1]+=rem
  14.         res=res[::-1]
  15.         print(res)
  16.         beg=-1
  17.         for i in range(m+n):
  18.             if res[i]!=0:
  19.                 beg=i
  20.                 break
  21.         if beg==-1:
  22.             return '0'
  23.         res=res[beg:]
  24.        
  25.         res=map(str,res)
  26.         return "".join(list(res))
  27.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement