Leetcode 43 Solution

This article provides solution to leetcode question 43 (multiply-strings)

https://leetcode.com/problems/multiply-strings

Solution

class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        arr = [0] * (len(num1) * len(num2) + 1)

        for i in range(len(num1)):
            for j in range(len(num2)):
                arr[i + j] += int(num1[len(num1) - 1 - i]) * int(num2[len(num2) - 1 - j])

        carry = 0
        for i in range(len(arr)):
            val = arr[i] + carry

            carry = val // 10
            val = val % 10

            arr[i] = val

        ans = ""
        for ch in reversed(arr):
            if ch == 0 and ans == '':
                continue
            ans += str(ch)
        return ans or '0'