Multiply Strings
Multiply two huge numbers written as digit strings
DSA, Medium, Math & Geometry
Which structure or algorithm, and what does it cost.
Two big whole numbers, a and b, arrive as strings of digits. Each can have 150 digits, too many for a normal number type. Write multiply(a, b) that returns their product as a string. Do not turn a whole string into a number. Work with one digit at a time. Do not pad the answer with zeros on the left. The only answer that starts with 0 is the string "0".
Examples
Constraints
- 1 <= length of
aand ofb<= 150. - Each string holds only the digits 0 to 9. A string starts with 0 only when it is exactly "0".
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n * m) time, where n and m are the lengths of the two strings.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Think of how you multiply two long numbers on paper. Which two digits make each small product?
Hint 2 the approach
A digit of a at one place, times a digit of b at another place, lands in a fixed place of the answer. Work out which place.
Hint 3 the key step
Make n + m cells. Use place values to work out which cell the product of digit i and digit j belongs in. Add every product first, then carry.
Walkthrough
Full walkthrough brute force to best
Brute force
Add a to itself b times, with digit-by-digit string addition. The number b can be as large as 10^150 - 1, so this can never finish. The cost is O(b * n) time.
Better method
Work as on paper. For each digit of b, from the right, multiply a by that digit to make a row, add that many zeros at its end, and add the row to a running total with string addition. There are m rows, and each addition costs O(n + m), so the time is O(m * (n + m)) and the memory is O(n + m).
Best method
Use one list of n + m cells for the answer. A digit of a, i places from the left, is worth 10 to the power n - 1 - i. A digit of b, j places from the left, is worth 10 to the power m - 1 - j. Their product is worth 10 to the power n + m - 2 - i - j, which is the place of cell i + j + 1 counted from the left. Add every digit product into its cell first. Then carry once, from right to left. A cell stays below 20,000 before the carry, so the carry is simple. Last, drop leading zeros, and return "0" if nothing is left.
Hand trace
Run on a = "47", b = "63". The list has 4 cells, all 0. Products: 7 times 3 is 21, into cell 3. 7 times 6 is 42, into cell 2. 4 times 3 is 12, into cell 2, which is now 54. 4 times 6 is 24, into cell 1. The cells are [0, 24, 54, 21]. Carry from the right. Cell 3 keeps 1 and gives 2 to cell 2, which becomes 56. Cell 2 keeps 6 and gives 5 to cell 1, which becomes 29. Cell 1 keeps 9 and gives 2 to cell 0. The cells are [2, 9, 6, 1]. The result is "2961".
Code
def multiply(a, b):
n, m = len(a), len(b)
cells = [0] * (n + m)
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
cells[i + j + 1] += int(a[i]) * int(b[j])
for k in range(n + m - 1, 0, -1):
cells[k - 1] += cells[k] // 10
cells[k] %= 10
text = ''.join(map(str, cells)).lstrip('0')
return text or '0'Time and memory
O(n * m) time: one product for each pair of digits, plus O(n + m) for the carry. O(n + m) memory for the cells.
Common mistakes
- Putting a product in cell i + j instead of cell i + j + 1. Every digit then lands one place too high.
- Forgetting to drop leading zeros, so 2 times 3 returns "06", the cells before the strip. Also, "0" times anything must give "0", not an empty string.
- Carrying from the left, or leaving a cell above 9 at the end.