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
1 <= num1.length, num2.length <= 200num1andnum2consist of digits only.- Both
num1andnum2do not contain any leading zero, except the number0itself.
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
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More Math / Number Theory problems
- Add BinaryEASY
- Factorial Trailing ZeroesMEDIUM
- Excel Sheet Column TitleEASY
- Greatest Common Divisor of StringsEASY
- Integer to RomanMEDIUM
- Palindrome NumberEASY
- Plus OneEASY
- Reverse IntegerMEDIUM
- Roman to IntegerEASY