mediumStringMathArrays & Hashing

Multiply Strings

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

Recognize the pattern

multiply two number stringsdigit-by-digit like paper multiplicationmanage positions and carries

Brute force idea

Not applicable — digit multiplication IS the approach.

Better approach

Result array of size m+n. For digits i,j: result[i+j+1] += num1[i] × num2[j]. Propagate carries. Strip leading zeros. O(m × n).

Key invariant

Product of digits at positions i and j contributes to result position i+j+1 with carry to i+j — mirrors paper multiplication.

Watch out for

Leading zeros in result — strip them but return '0' if all zeros.

Arrays & Hashing Pattern