43. Multiply Strings
At a Glance
- Topic: math-geometry
- Pattern: String math (grade-school multiplication)
- Difficulty: Medium
- Companies: Facebook, Amazon, Microsoft, Google, Twitter
- Frequency: Medium
- LeetCode: 43
Problem (one-liner)
Given two non-negative integer strings first and second without leading zeros (except "0"), return their product as a string without using built-in big integer libraries.
Recognition Cues
- "Multiply strings" / numbers too large for 64-bit
- Digit-by-digit convolution length
len(first)+len(second)-1positions
Diagram
At-a-glance flow (replace with problem-specific Mermaid as you refine this note). camelCase node IDs; no spaces in IDs.
Loading diagram…
Approaches
- Brute force — convert with bigint — violates constraint / language-dependent.
- Optimal — array of length
len(first)+len(second), accumulatedigit_i * digit_jintoresult[i+j+1]with carries —O(n·m)time /O(n+m)space.
Optimal Solution
Go
package main
func multiply(first string, second string) string {
if first == "0" || second == "0" {
return "0"
}
digits := make([]int, len(first)+len(second))
for leftIndex := len(first) - 1; leftIndex >= 0; leftIndex-- {
for rightIndex := len(second) - 1; rightIndex >= 0; rightIndex-- {
product := int(first[leftIndex]-'0')*int(second[rightIndex]-'0') + digits[leftIndex+rightIndex+1]
digits[leftIndex+rightIndex+1] = product % 10
digits[leftIndex+rightIndex] += product / 10
}
}
start := 0
for start < len(digits) && digits[start] == 0 {
start++
}
result := make([]byte, len(digits)-start)
for offset := range result {
result[offset] = byte('0' + digits[start+offset])
}
return string(result)
}JavaScript
function multiply(first, second) {
if (first === '0' || second === '0') {
return '0';
}
const digitCount = first.length + second.length;
const digits = new Array(digitCount).fill(0);
for (let leftIndex = first.length - 1; leftIndex >= 0; leftIndex--) {
for (let rightIndex = second.length - 1; rightIndex >= 0; rightIndex--) {
const product =
(first.charCodeAt(leftIndex) - 48) * (second.charCodeAt(rightIndex) - 48) +
digits[leftIndex + rightIndex + 1];
digits[leftIndex + rightIndex + 1] = product % 10;
digits[leftIndex + rightIndex] += Math.floor(product / 10);
}
}
let start = 0;
while (start < digits.length && digits[start] === 0) {
start++;
}
return digits.slice(start).join('');
}Walkthrough
first="12", second="34" → positions for partial products accumulate into digits:
| cell | contribution |
|---|---|
| index 3 | 2*4 |
| index 2 | 1*4 + 2*3 plus carries |
| index 1 | 1*3 plus carries |
Invariant: after nested loops, each digits[k] is a base-10 digit after propagation.
Edge Cases
"0"times anything- Single-digit operands
- Long strings — output length sum of lengths or shorter if leading zeros trimmed
Pitfalls
- Off-by-one in index mapping (
i+jvsi+j+1depending on carry slot) - Forgetting to strip leading zero bucket
Similar Problems
- 066. Plus One — carry propagation on digits.
- 050. Pow(x, n) — large composite arithmetic mindset.
Variants
- Add two strings (same carry machinery).
- Karatsuba (theoretical improvement — rarely needed in interviews).
Mind-Map Tags
#string-math #grade-school #carry #digit-array #multiply
Last updated on
Spotted something unclear or wrong on this page?