Advertisement

Add Strings - LeetCode 415 Solution

Add Strings - Complete Solution Guide

Add Strings is LeetCode problem 415, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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

Detailed Explanation

The problem requires us to add two non-negative integers, `num1` and `num2`, which are given as strings. We must return the sum of these two numbers also as a string. The crucial constraint is that we cannot use built-in libraries for handling large integers or directly convert the input strings to integers. This forces us to simulate the addition process digit by digit, similar to how we perform addition manually.

Solution Approach

The provided solutions simulate the addition of two numbers represented as strings. They iterate through the strings from right to left, adding the corresponding digits along with any carry from the previous addition. The resulting digits are then appended to a string builder (or similar data structure) to form the final sum string. The key is to handle the carry correctly and to ensure that shorter strings are treated as if they are padded with leading zeros.

Step-by-Step Algorithm

  1. Step 1: Initialize pointers `i` and `j` to the last index of `num1` and `num2`, respectively. Also, initialize `carry` to 0 and create an empty string or string builder to store the result.
  2. Step 2: Iterate as long as either `i` or `j` is non-negative or the `carry` is greater than 0.
  3. Step 3: In each iteration, extract the digits from `num1` and `num2` at the current indices `i` and `j`. If an index is out of bounds (i.e., `i < 0` or `j < 0`), treat the digit as 0.
  4. Step 4: Calculate the sum of the two digits and the `carry`.
  5. Step 5: Calculate the new `carry` by dividing the sum by 10. The remainder of the division is the digit to be appended to the result.
  6. Step 6: Append the digit (remainder) to the result string/string builder.
  7. Step 7: Decrement `i` and `j` to move to the next digits (from right to left).
  8. Step 8: If using a string builder, reverse the resulting string before returning it. Otherwise, if concatenating to a string, prepend to the result.

Key Insights

  • Insight 1: The core idea is to simulate the manual addition process, starting from the least significant digits (rightmost characters) of the input strings.
  • Insight 2: Maintaining a `carry` variable is essential to handle cases where the sum of two digits exceeds 9.
  • Insight 3: The length of the result can be at most one more than the maximum length of the two input strings (due to a possible carry-over at the end).

Complexity Analysis

Time Complexity: O(max(n, m))

Space Complexity: O(max(n, m))

Topics

This problem involves: Math, String, Simulation.

Companies

Asked at: Airbnb, Avito, Capital One, Jane Street, Wayfair.