Skip to main content

Add Strings - Solution & Explanation

EasyMathStringSimulation25 min readAsked at: Amazon, Microsoft, Meta +11
Practice this problem

Problem Statement

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.

Approach Overview

Problem Overview: You are given two non‑negative integers stored as strings. The task is to return their sum, also as a string, without converting the entire string into an integer. The challenge is handling digit addition and carry manually, similar to how you add numbers on paper.

Approach 1: Reverse and Traverse (O(n) time, O(n) space)

This method reverses both strings so the least significant digits appear first. After reversing, iterate from index 0 while adding corresponding digits and maintaining a carry. Each step computes digitSum = d1 + d2 + carry, appends digitSum % 10 to the result, and updates carry = digitSum / 10. Continue until both strings are fully processed and the carry is handled. The result string is reversed again to restore correct order. Time complexity is O(n) because every digit is processed once, and space complexity is O(n) due to storing reversed strings and the output.

This approach is straightforward and mirrors manual addition closely. Reversing the strings simplifies index handling because digits align naturally from least significant to most significant.

Approach 2: Character by Character Addition Using Two Pointers (O(n) time, O(1) extra space)

This approach avoids reversing strings by using two pointers starting from the end of each string. Pointer i begins at num1.length() - 1 and pointer j at num2.length() - 1. At each step, convert characters to digits, compute their sum with the current carry, and append the resulting digit. Decrement pointers and repeat until both pointers are exhausted and no carry remains.

The digits are generated from least significant to most significant, so the result is typically built using a dynamic buffer and reversed once at the end. Time complexity remains O(n), where n is the maximum length of the two strings. Extra space is O(1) excluding the output string. This method is commonly preferred because it avoids modifying the input and keeps memory overhead minimal.

The problem mainly tests understanding of string manipulation, elementary math operations, and iterative simulation of digit-by-digit addition.

Recommended for interviews: The two‑pointer approach is the expected solution. Interviewers want to see that you can simulate addition from right to left while managing carry efficiently. The reverse‑and‑traverse method still demonstrates correct reasoning, but the pointer approach shows stronger control over string traversal and space optimization.

Approach 1: Reverse and Traverse

This approach involves reversing both input strings, then iterating through them to sum up each digit, similar to manual addition from the rightmost digit. This technique simplifies handling the carry over during addition.

The function addStrings adds two numbers represented as strings without converting them directly into integers. It accomplishes this by iterating over the digits in reverse, calculating the sum, and storing this in a result string. We keep the process efficient by using a simple loop to handle differing lengths of input strings, adding the carry and reversing the string at the end to present the final sum.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the maximum length of num1 or num2.
Space Complexity: O(n), for the result array.

Try this approach in the editor →

Approach 2: Character by Character Addition Using Two Pointers

This approach deploys two pointers, initially positioned at the end of each input string. We process each character one by one, moving the pointers from the rightmost end towards the start of each string. This allows us to readily manage the carry as we compute the sum step-by-step.

This C implementation utilizes dynamic memory allocation to handle the expected length of the result string. By manipulating pointers, the implementation skillfully confines carry operations within the loop. The function returns a pointer to the correct start of the result string after adjusting for any leading zeros produced by initial carry operations.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the maximum length of num1 or num2.
Space Complexity: O(n), necessary for the dynamically allocated result string.

Try this approach in the editor →

Approach 3: Two Pointers

We use two pointers i and j to point to the end of the two strings respectively, and start adding bit by bit from the end. Each time we take out the corresponding digits a and b, calculate their sum a + b + c, where c represents the carry from the last addition. Finally, we append the units digit of a + b + c to the end of the answer string, and then take the tens digit of a + b + c as the value of the carry c, and loop this process until the pointers of both strings have pointed to the beginning of the string and the value of the carry c is 0.

Finally, reverse the answer string and return it.

The time complexity is O(max(m, n)), where m and n are the lengths of the two strings respectively. Ignoring the space consumption of the answer string, the space complexity is O(1).

The following code also implements string subtraction, refer to the subStrings(num1, num2) function.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Kotlin

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Reverse and Traverse

Time Complexity: O(n), where n is the maximum length of num1 or num2.
Space Complexity: O(n), for the result array.

Character by Character Addition Using Two Pointers

Time Complexity: O(n), where n is the maximum length of num1 or num2.
Space Complexity: O(n), necessary for the dynamically allocated result string.

Two Pointers

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Reverse and TraverseO(n)O(n)When simplicity matters and reversing strings is acceptable
Two Pointers from EndO(n)O(1) extraBest general solution; avoids reversing and minimizes memory usage

Video Solution

Add Strings | Leetcode 415 Solution in HindiPepcoding14,171 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Add Strings easy or hard?
Add Strings is classified as an Easy problem on LeetCode. The logic mirrors elementary school addition but requires careful handling of carry, pointer movement, and string construction without converting the entire number to an integer.
How to solve Add Strings in O(n)?
Traverse both strings from the last character using two pointers. Convert each character to its numeric digit, add them with a carry value, and append the result digit. Continue until both pointers are exhausted and handle any remaining carry. Each digit is processed once, giving O(n) time complexity.
What is the best approach for Add Strings?
The two‑pointer approach starting from the end of each string is considered the best solution. It simulates manual addition by processing digits from right to left while maintaining a carry value. The algorithm runs in O(n) time and uses O(1) extra space excluding the output string.
What data structure is used in Add Strings?
The solution mainly uses string traversal and a dynamic result buffer such as a string builder or list. Two integer pointers track positions in the input strings, and a carry variable maintains overflow between digit additions.
What is the time complexity of Add Strings?
The time complexity is O(n), where n is the maximum length of the two input strings. Each digit from both strings is processed once during the addition simulation. Space complexity is O(1) extra if using the two‑pointer approach, aside from the result string.
Add Strings Python or Java solution approach
In both Python and Java, iterate from the end of each string using indices. Convert characters to digits using subtraction like `c - '0'` in Java or `int(c)` in Python, compute the sum with carry, append the result digit, and finally reverse the constructed string.
Is Add Strings asked at Google, Amazon, or Meta?
Add Strings is a common interview-style question used by companies like Amazon, Google, and Meta to test string manipulation and basic algorithmic thinking. It evaluates whether candidates can simulate arithmetic operations without relying on built-in big integer conversions.

Ready to solve this problem?

Practice Add Strings with our built-in code editor and test cases.

Practice on FleetCode