Add Strings

Easy
163.

Add Strings

Add Strings is an easy coding problem on strings, math and simulation, asked in Amazon, Google, Meta and Airbnb interviews.

Given two non-negative integers num1 and num2 represented as strings, return their sum as a string.

You must solve the problem without converting the inputs to integers directly and without using any big-integer library.

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 <= 20
  • num1 and num2 consist of digits only.
  • Neither has a leading zero, except the value 0 itself.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var addStrings = function(num1, num2) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Add Strings — Solution in Python, Java & C++

Add Strings solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at Amazon and Google.

Add Strings is an easy coding problem on strings, math and simulation, asked in Amazon, Google, Meta and Airbnb interviews.

Problem statement

Given two non-negative integers num1 and num2 represented as strings, return their sum as a string.

You must solve the problem without converting the inputs to integers directly and without using any big-integer library.

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 <= 20
  • num1 and num2 consist of digits only.
  • Neither has a leading zero, except the value 0 itself.

Hints for Add Strings

  1. Hint 1

    Column addition from the right, carrying as you go.

  2. Hint 2

    Treat a missing digit in the shorter string as 0 rather than special-casing the lengths.

  3. Hint 3

    The loop must survive a final carry — "9" + "1" is "10", one character longer than either input.

How to solve Add Strings

Schoolbook addition from the right with a carry, treating a missing digit in the shorter string as zero.

Approach

  1. Set i, j to the last indices of the two strings and carry = 0.
  2. While i >= 0 or j >= 0 or carry: sum the carry and the available digits, append sum % 10, set carry = sum // 10.
  3. Reverse the collected digits and join.

Why it works

Each column produces a digit in 0…9 and a carry of 0 or 1. Processing columns right to left with the carry threaded through is the definition of decimal addition, and the loop condition ensures a leading carry is emitted.

Complexity

  • Time — O(max(len a, len b))
  • Space — O(max(len a, len b))

Pitfalls

  • Use ord(ch) - 48 (or the language's equivalent) rather than parsing each character.
  • "9" + "1" is "10" — the result can be longer than both inputs.

Reference solution

Python

def addStrings(num1: str, num2: str) -> str:
    i, j, carry = len(num1) - 1, len(num2) - 1, 0
    out = []
    while i >= 0 or j >= 0 or carry:
        total = carry
        if i >= 0:
            total += ord(num1[i]) - 48
            i -= 1
        if j >= 0:
            total += ord(num2[j]) - 48
            j -= 1
        out.append(str(total % 10))
        carry = total // 10
    return "".join(reversed(out))

Java

public static String addStrings(String num1, String num2) {
    int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
    StringBuilder sb = new StringBuilder();
    while (i >= 0 || j >= 0 || carry > 0) {
        int total = carry;
        if (i >= 0) { total += num1.charAt(i) - '0'; i--; }
        if (j >= 0) { total += num2.charAt(j) - '0'; j--; }
        sb.append((char) ('0' + total % 10));
        carry = total >= 10 ? 1 : 0;
    }
    return sb.reverse().toString();
}

C++

string addStrings(string num1, string num2) {
    int i = (int) num1.size() - 1, j = (int) num2.size() - 1, carry = 0;
    string out = "";
    while (i >= 0 || j >= 0 || carry > 0) {
        int total = carry;
        if (i >= 0) { total += num1[i] - '0'; i--; }
        if (j >= 0) { total += num2[j] - '0'; j--; }
        out += (char) ('0' + total % 10);
        carry = total >= 10 ? 1 : 0;
    }
    reverse(out.begin(), out.end());
    return out;
}

JavaScript

var addStrings = function(num1, num2) {
    let i = num1.length - 1, j = num2.length - 1, carry = 0, out = "";
    while (i >= 0 || j >= 0 || carry > 0) {
        let total = carry;
        if (i >= 0) { total += num1.charCodeAt(i) - 48; i--; }
        if (j >= 0) { total += num2.charCodeAt(j) - 48; j--; }
        out = String(total % 10) + out;
        carry = total >= 10 ? 1 : 0;
    }
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings