AAlgoLoopSpaced repetition for LeetCode
MEDIUMMath / Number TheoryLeetCode ↗

Multiply Strings

The key idea

Mimic grade-school multiplication on a digit array. The product of num1[i] and num2[j] always lands in result positions i + j and i + j + 1, so you can accumulate every partial product directly into one fixed-size array of length m + n without ever converting the strings to integers.

Problem

Given 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 an integer directly.

Constraints

Examples

Input: num1 = "2", num2 = "3" Output: "6"
Input: num1 = "123", num2 = "456" Output: "56088"
Input: num1 = "12", num2 = "12" Output: "144"

Complexity

Time: O(m * n) Space: O(m + n)

See the full solution

410310
Step-by-step visualization
Start free →

More Math / Number Theory problems