Problem
Section titled “Problem”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:
num1andnum2consist of digits only.- Both
num1andnum2do not contain any leading zero, except the number0itself.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”The inputs can have 200 digits, far beyond any built-in integer type, so we do long multiplication by hand. Instead of building each partial product as a string and adding strings together, notice where each digit product lands.
Let m = num1.size() and n = num2.size(). Index digits from the left, so num1[i] has place value and num2[j] has place value . Their product has place value . The product has at most m + n digits; in an array res of that length, indexed from the left, that place value is exactly position i + j + 1. So:
every digit product
num1[i] * num2[j]is added intores[i + j + 1], and any carry goes intores[i + j].
If we process i and j from right to left, positions are filled from least significant upwards. At each step we add the product to the current value of res[i + j + 1], keep the last digit there, and push the tens into res[i + j]. Each res[k] may temporarily exceed 9, but it is normalised again when a later step (smaller i + j) adds into it and carries out of it, so at the end every cell is a single digit.
Algorithm
Section titled “Algorithm”- If either number is
"0", return"0". - Let
resbe an array ofm + nzeros. - For
ifromm - 1down to0, andjfromn - 1down to0:sum = (num1[i] - '0') * (num2[j] - '0') + res[i + j + 1];res[i + j + 1] = sum % 10;res[i + j] += sum / 10.
- Convert
resto a string, skipping leading zeros (there is at most one, atres[0]).
Walkthrough
Section titled “Walkthrough”Example 2, 123 × 456. The blue cell is res[i + j + 1] receiving the product, the orange cell res[i + j] receives the carry:
Complexity
Section titled “Complexity”- Time: — one constant-time update per pair of digits, plus a linear pass to build the string. (Adding whole partial-product strings would cost .)
- Space: — the
resarray (also the size of the answer).
Edge cases
Section titled “Edge cases”- A zero operand (
"0" × "456"): returned as"0"straight away; otherwise the leading-zero skip would produce an empty string. - No carry into the top position (
"2" × "3"givesres = [0, 6]): the leading0is skipped. - Large intermediate values: a cell holds at most
81 + 9 + carry, sointnever overflows.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: string multiply(string num1, string num2) { if (num1 == "0" || num2 == "0") return "0"; int m = num1.size(), n = num2.size(); vector<int> res(m + n, 0); for (int i = m - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { int sum = (num1[i] - '0') * (num2[j] - '0') + res[i + j + 1]; res[i + j + 1] = sum % 10; res[i + j] += sum / 10; } } string ans; for (int d : res) if (!(ans.empty() && d == 0)) ans += char('0' + d); return ans; }};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 sum = (num1.charAt(i) - '0') * (num2.charAt(j) - '0') + res[i + j + 1]; res[i + j + 1] = sum % 10; res[i + j] += sum / 10; } } StringBuilder ans = new StringBuilder(); for (int d : res) if (!(ans.length() == 0 && d == 0)) ans.append(d); return ans.toString(); }}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): a = int(num1[i]) for j in range(n - 1, -1, -1): s = a * int(num2[j]) + res[i + j + 1] res[i + j + 1] = s % 10 res[i + j] += s // 10 return "".join(map(str, res)).lstrip("0")Give it a try first
Attempt the problem on LeetCode — the code will still be here.










