Multiply Strings — LeetCode #43

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Constraints:

  • 1 <= num1.length, num2.length <= 200

  • num1 and num2 consist of digits only.

  • Both num1 and num2 do not contain any leading zero, except the number 0 itself.

Solutions:

Python

class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        if num1 == "0" or num2 == "0":
            return "0"

        m, n = len(num1), len(num2)
        res = [0] * (m + n)

        for i in range(m - 1, -1, -1):
            for j in range(n - 1, -1, -1):
                mul = int(num1[i]) * int(num2[j])
                p1, p2 = i + j, i + j + 1
                mul += res[p2]
                res[p1] += mul // 10
                res[p2] = mul % 10

        sb = []
        for i in res:
            if len(sb) != 0 or i != 0:
                sb.append(str(i))

        return "".join(sb)

C#

public class Solution {
    public string Multiply(string num1, string num2) {
        if (num1 == "0" || num2 == "0") {
            return "0";
        }

        int m = num1.Length, n = num2.Length;
        int[] res = new int[m + n];

        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                int mul = (num1[i] - '0') * (num2[j] - '0');
                int p1 = i + j, p2 = i + j + 1;
                mul += res[p2];
                res[p1] += mul / 10;
                res[p2] = mul % 10;
            }
        }

        StringBuilder sb = new StringBuilder();
        foreach (int i in res) {
            if (sb.Length != 0 || i != 0) {
                sb.Append(i);
            }
        }

        return sb.ToString();
    }
}

Java

class Solution {
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }

        int m = num1.length(), n = num2.length();
        int[] res = new int[m + n];

        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                int p1 = i + j, p2 = i + j + 1;
                mul += res[p2];
                res[p1] += mul / 10;
                res[p2] = mul % 10;
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i : res) {
            if (sb.length() != 0 || i != 0) {
                sb.append(i);
            }
        }

        return sb.toString();
    }
}

Javascript

/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var multiply = function(num1, num2) {
    if (num1 === "0" || num2 === "0") {
        return "0";
    }

    const m = num1.length, n = num2.length;
    const res = new Array(m + n).fill(0);

    for (let i = m - 1; i >= 0; i--) {
        for (let j = n - 1; j >= 0; j--) {
            let mul = (num1.charCodeAt(i) - 48) * (num2.charCodeAt(j) - 48);
            const p1 = i + j, p2 = i + j + 1;
            mul += res[p2];
            res[p1] += Math.floor(mul / 10);
            res[p2] = mul % 10;
        }
    }

    let sb = "";
    for (let i of res) {
        if (sb.length !== 0 || i !== 0) {
            sb += i;
        }
    }

    return sb;
};

Typescript

function multiply(num1: string, num2: string): string {
    if (num1 === "0" || num2 === "0") {
        return "0";
    }

    const m = num1.length, n = num2.length;
    const res = new Array(m + n).fill(0);

    for (let i = m - 1; i >= 0; i--) {
        for (let j = n - 1; j >= 0; j--) {
            let mul = (num1.charCodeAt(i) - 48) * (num2.charCodeAt(j) - 48);
            const p1 = i + j, p2 = i + j + 1;
            mul += res[p2];
            res[p1] += Math.floor(mul / 10);
            res[p2] = mul % 10;
        }
    }

    let sb = "";
    for (let i of res) {
        if (sb.length !== 0 || i !== 0) {
            sb += i;
        }
    }

    return sb;
}

I hope this helps! Let me know if you have any questions. Don’t forget to follow and give some like to support my content

https://medium.com/@araneznorman

Did you find this article valuable?

Support Norman Aranez by becoming a sponsor. Any amount is appreciated!