#43
Multiply Strings
medium· Math & Geometryruns: 0Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. You must not use any built-in BigInteger library or convert the inputs to integers directly.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 657
class Solution:
def multiply(self, num1: str, num2: str) -> str:
if num1 == "0" or num2 == "0":
return "0"
m, n = len(num1), len(num2)
result = [0] * (m + n)
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
product = int(num1[i]) * int(num2[j])
p1, p2 = i + j, i + j + 1
total = product + result[p2]
result[p1] += total // 10
result[p2] = total % 10
start = 0
while start < len(result) and result[start] == 0:
start += 1
return "".join(str(d) for d in result[start:])
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.