415. Add Strings
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to add two non-negative integers represented as strings and return their sum as a string. The key constraints are that we cannot use built-in libraries for large number arithmetic (like BigInteger) and we cannot convert the entire input strings to integers directly. This is because the numbers can be very large, exceeding the capacity of standard integer types like int or long long.
Solution
Add Strings in Python
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
res = []
i, j = len(num1) - 1, len(num2) - 1
carry = 0
# Loop as long as there are digits in either string, or a carry remains.
while i >= 0 or j >= 0 or carry:
# Get the digit from num1. If the pointer is out of bounds, use 0.
d1 = int(num1[i]) if i >= 0 else 0
# Get the digit from num2. If the pointer is out of bounds, use 0.
d2 = int(num2[j]) if j >= 0 else 0
# Calculate the sum for the current position.
total = d1 + d2 + carry
# The digit to append to the result is the last digit of the sum.
res.append(str(total % 10))
# The new carry is the tens digit of the sum (integer division).
carry = total // 10
# Move pointers to the left.
i -= 1
j -= 1
# The result list has digits in reverse order. Reverse and join to form the final string.
return "".join(reversed(res))
Add Strings in Java
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder res = new StringBuilder();
int i = num1.length() - 1;
int j = num2.length() - 1;
int carry = 0;
// Loop as long as there are digits in either string, or a carry remains.
while (i >= 0 || j >= 0 || carry > 0) {
// Get the digit from num1. If the pointer is out of bounds, use 0.
int d1 = (i >= 0) ? num1.charAt(i) - '0' : 0;
// Get the digit from num2. If the pointer is out of bounds, use 0.
int d2 = (j >= 0) ? num2.charAt(j) - '0' : 0;
// Calculate the sum for the current position.
int sum = d1 + d2 + carry;
// The digit to append to the result is the last digit of the sum.
res.append(sum % 10);
// The new carry is the tens digit of the sum.
carry = sum / 10;
// Move pointers to the left.
i--;
j--;
}
// The result is built in reverse, so we reverse it and convert to a String.
return res.reverse().toString();
}
}
Add Strings in C++
class Solution {
public:
string addStrings(string num1, string num2) {
string res = "";
int i = num1.length() - 1;
int j = num2.length() - 1;
int carry = 0;
// Loop as long as there are digits in either string, or a carry remains.
while (i >= 0 || j >= 0 || carry > 0) {
// Get the digit from num1. If the pointer is out of bounds, use 0.
int d1 = (i >= 0) ? num1[i] - '0' : 0;
// Get the digit from num2. If the pointer is out of bounds, use 0.
int d2 = (j >= 0) ? num2[j] - '0' : 0;
// Calculate the sum for the current position.
int sum = d1 + d2 + carry;
// The digit to append to the result is the last digit of the sum.
res += to_string(sum % 10);
// The new carry is the tens digit of the sum.
carry = sum / 10;
// Move pointers to the left.
i--;
j--;
}
// The result is built in reverse, so we need to reverse it before returning.
reverse(res.begin(), res.end());
return res;
}
};
Add Strings in TypeScript
function addStrings(num1: string, num2: string): string {
let res: string[] = [];
let i = num1.length - 1;
let j = num2.length - 1;
let carry = 0;
// Loop as long as there are digits in either string, or a carry remains.
while (i >= 0 || j >= 0 || carry > 0) {
// Get the digit from num1. If the pointer is out of bounds, use 0.
const d1 = i >= 0 ? parseInt(num1[i]) : 0;
// Get the digit from num2. If the pointer is out of bounds, use 0.
const d2 = j >= 0 ? parseInt(num2[j]) : 0;
// Calculate the sum for the current position.
const sum = d1 + d2 + carry;
// The digit to append to the result is the last digit of the sum.
res.push((sum % 10).toString());
// The new carry is the tens digit of the sum.
carry = Math.floor(sum / 10);
// Move pointers to the left.
i--;
j--;
}
// The result array has digits in reverse order. Reverse and join to form the final string.
return res.reverse().join('');
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.