Multiply Two Numbers Represented as Strings
Implement multiplyStrings
Given two non-negative integers
num1 and num2 represented as strings, return their product as a string — without ever converting the inputs to native integers, since they may be arbitrarily large.
The naive approach mirrors long multiplication exactly: multiply num1 by each digit of num2 in turn, shift each partial product into place with trailing zeros, and add everything together with repeated string addition. It works, but every digit triggers a fresh full-length addition. The position arrayPosition ArrayA single array sized to hold every possible digit of the final product, where each pair of input digits is accumulated directly into its known destination positions — no intermediate partial-product strings ever need to be built. technique skips that: since multiplying the digit at position i by the digit at position j always lands at result positions i+j and i+j+1, every digit pair can be accumulated directly into one shared array in a single pass.
Example 1:
Input: num1 = "123", num2 = "9"
Output: "1107"
Example 2:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 3:
Input: num1 = "0", num2 = "12345"
Output: "0"
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ num1.length, num2.length ≤ 200 - ●
num1 and num2 consist of digits only - ●
Neither num1 nor num2 has a leading zero, except the number "0" itself - ●
You may NOT convert the inputs directly to native integers and multiply — they may be arbitrarily large, larger than any fixed-width integer type
num1 =
123
num2 =
9