Add Strings - Problem

Imagine you're working with incredibly large numbers that are too big for standard integer types - numbers with hundreds or thousands of digits! Your task is to add two such numbers represented as strings without using any built-in big integer libraries.

Given two non-negative integers num1 and num2 represented as strings, return their sum as a string. You must solve this problem by simulating manual addition - just like how you learned to add numbers digit by digit in elementary school, carrying over when needed.

Constraints:

  • You cannot convert the entire strings to integers directly
  • You cannot use built-in big integer libraries
  • Both numbers are non-negative
  • No leading zeros except for the number 0 itself

Example: "123" + "456" = "579"

Input & Output

example_1.py โ€” Basic Addition
$ Input: num1 = "11", num2 = "23"
โ€บ Output: "34"
๐Ÿ’ก Note: Adding 11 + 23 = 34. Start from rightmost: 1+3=4, then 1+2=3, result is "34"
example_2.py โ€” Addition with Carry
$ Input: num1 = "456", num2 = "77"
โ€บ Output: "533"
๐Ÿ’ก Note: Adding 456 + 77 = 533. Rightmost: 6+7=13 (write 3, carry 1), middle: 5+7+1=13 (write 3, carry 1), leftmost: 4+0+1=5, result is "533"
example_3.py โ€” Different Length Strings
$ Input: num1 = "0", num2 = "0"
โ€บ Output: "0"
๐Ÿ’ก Note: Edge case: adding two zeros results in "0"

Visualization

Tap to expand
String Addition: "123" + "456" = "579"Step 1: Initialize pointers123i456jcarry = 0Step 2-4: Add digit by digit3 + 6 + 0 = 9 โ†’ result = "9", carry = 02 + 5 + 0 = 7 โ†’ result = "79", carry = 01 + 4 + 0 = 5 โ†’ result = "579", carry = 0579Final Result: "579"Algorithm Steps1. Start from rightmost digits2. Add digits + carry3. Store result digit (sum % 10)4. Update carry (sum // 10)5. Move pointers left6. Repeat until doneTime: O(max(m,n))
Understanding the Visualization
1
Initialize
Set up pointers at the end of both strings, initialize carry to 0
2
Process Digits
Add current digits plus carry, store the units digit in result
3
Handle Carry
Calculate new carry value and move to next position
4
Continue
Repeat until all digits processed and no carry remains
Key Takeaway
๐ŸŽฏ Key Insight: Simulate manual addition by processing digits right-to-left with proper carry handling - exactly like elementary school math!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(max(m, n))

We process each digit once, where m and n are lengths of input strings

n
2n
โœ“ Linear Growth
Space Complexity
O(max(m, n))

Space needed to store the result string, which can be at most max(m,n) + 1 digits

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค num1.length, num2.length โ‰ค 104
  • num1 and num2 consist of only digits
  • num1 and num2 don't have any leading zeros except for the zero itself
  • Cannot use built-in big integer libraries
  • Cannot convert strings directly to integers
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
89.5K Views
High Frequency
~18 min Avg. Time
2.7K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen