415. Add Strings

Add Strings (LeetCode 415) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the String Manipulation Patterns pattern, in the Multiply Strings (Manual Simulation) subpattern.

How to solve Add Strings

Add Strings is a Multiply Strings problem, part of String Manipulation Patterns. Learn to multiply large numbers represented as strings using manual simulation of multiplication.

Work towards the solution in these steps:

  1. Think about how you add numbers manually, digit by digit, and how that process can be simulated using strings.
  2. Since the input numbers are strings, consider iterating from the end of each string, processing one digit at a time.
  3. Keep track of any carry generated during addition and how it affects the sum of the next pair of digits.

The reference solution runs in O(n) time and O(1) space.

The Add Strings solution and editorial walks through the full approach with reference code.

Problem statement

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

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 <= 10^4 - num1 and num2 consist of only digits. - num1 and num2 don't have any leading zeros except for the zero itself.

Follow up: Could you solve it without using any built-in BigInteger library or converting the inputs to integer directly?

Topics and companies

Topics: String.

Reported in interviews at Facebook, Adobe, Google, Wayfair, Microsoft, Square, Uber, Bloomberg, Amazon, Oracle.

How to practise Add Strings on Thita.ai

Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Add Strings in the code editor, or read the Add Strings editorial for a worked solution with its approach and complexity analysis.

Where Add Strings sits in the DSA pattern sheet

Problems related to Add Strings

Other problems that use the same Multiply Strings (Manual Simulation) and String Manipulation Patterns techniques:

Add Strings FAQ

What is the LeetCode number of Add Strings?

Add Strings is LeetCode problem 415, rated Easy.

What pattern is used to solve Add Strings?

Add Strings is a Multiply Strings problem. It belongs to the String Manipulation Patterns pattern, in the Multiply Strings (Manual Simulation) subpattern.

What is the time complexity of Add Strings?

The reference solution runs in O(n) time and O(1) space.

Which companies ask Add Strings?

Add Strings has been reported in interviews at Facebook, Adobe, Google, Wayfair, Microsoft, Square, Uber, Bloomberg, Amazon, Oracle.

Preparing this workspace.