Given two non-negative integers, num1
and num2
represented as string, return the sum of num1
and num2
as a string.
You must solve the problem without using any built-in library for handling large integers (such as BigInteger
). You must also not convert the inputs to integers directly.
Example 1:
Input: num1 = "11", num2 = "123" Output: "134"
Example 2:
Input: num1 = "456", num2 = "77" Output: "533"
Example 3:
Input: num1 = "0", num2 = "0" Output: "0"
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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
When adding strings representing numbers, the brute force approach mimics how we add numbers by hand, digit by digit. We start from the rightmost digits of both strings and work our way left, keeping track of any carry-over values. This continues until we've processed all digits of both strings and any remaining carry.
Here's how the algorithm would work step-by-step:
def add_strings(number1, number2):
result = ""
carry = 0
pointer1 = len(number1) - 1
pointer2 = len(number2) - 1
while pointer1 >= 0 or pointer2 >= 0:
digit1 = int(number1[pointer1]) if pointer1 >= 0 else 0
digit2 = int(number2[pointer2]) if pointer2 >= 0 else 0
# Sum the digits and the carry
current_sum = digit1 + digit2 + carry
carry = current_sum // 10
current_digit = current_sum % 10
result = str(current_digit) + result
if pointer1 >= 0:
pointer1 -= 1
if pointer2 >= 0:
pointer2 -= 1
# If there's a carry left, add it to the result
if carry:
result = str(carry) + result
return result
We're adding two very large numbers that are represented as strings, so we can't directly use typical addition. We'll mimic the way we add numbers by hand, working from right to left, keeping track of any carry-over values.
Here's how the algorithm would work step-by-step:
def add_strings(number1, number2):
result = ''
carry = 0
pointer1 = len(number1) - 1
pointer2 = len(number2) - 1
while pointer1 >= 0 or pointer2 >= 0:
digit1 = int(number1[pointer1]) if pointer1 >= 0 else 0
digit2 = int(number2[pointer2]) if pointer2 >= 0 else 0
# Perform digit-wise addition with carry
current_sum = digit1 + digit2 + carry
carry = current_sum // 10
current_digit = current_sum % 10
result += str(current_digit)
if pointer1 >= 0:
pointer1 -= 1
if pointer2 >= 0:
pointer2 -= 1
# Add the final carry if it exists
if carry:
result += str(carry)
# Reverse the result string
return result[::-1]
Case | How to Handle |
---|---|
Both num1 and num2 are empty strings | Return '0' or an empty string, depending on specification, after checking if both strings are empty. |
One of the strings is empty | Return the non-empty string as the result, since adding to empty string doesn't change value. |
num1 or num2 starts with leading zeros | The algorithm should correctly handle the leading zeros as they are digits in the strings. |
Very large input strings that might cause integer overflow if converted to integers | The algorithm should perform digit-by-digit addition as strings to avoid integer overflow. |
One string is significantly longer than the other | The algorithm must correctly handle different lengths by appropriately iterating through both strings and handling the carry. |
The sum results in a carry at the most significant digit | The algorithm needs to correctly prepend the carry to the result string. |
Both strings are '0' | The solution should return '0' without errors. |
String containing non-digit characters | The function should validate the input strings and either throw an error or return an appropriate message if they contain non-digit characters. |