147. Long Multiplication
A school app shows very large numbers as text. Given two non-negative integers a and b, each written as a string of digits, return their product, also as a string. The strings may start with extra zeros, but the answer must not: it has no leading zeros, and a product of zero is written as just 0.
The numbers can have up to 2000 digits, so you must not turn the whole strings into built-in integer values. Multiply the way it is taught at school: each digit of one number with each digit of the other, adding into the right place and carrying.
Example 1
- Input:
- a = "125", b = "8"
- Output:
- "1000"
- Explanation:
125 times 8 is 1000.
Example 2
- Input:
- a = "0012", b = "3400"
- Output:
- "40800"
- Explanation:
The leading zeros are ignored: 12 times 3400 is 40800.
Constraints
1 ≤ a.length, b.length ≤ 2000
a and b contain only the digits 0 to 9 and may have leading zeros
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(n * m)
- Space
- O(n + m)