415. Add Strings LeetCode Solution

In this guide, you will get 415. Add Strings LeetCode Solution with the best time and space complexity. The solution to Add Strings problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Add Strings solution in C++
  4. Add Strings solution in Java
  5. Add Strings solution in Python
  6. Additional Resources
415. Add Strings LeetCode Solution image

Problem Statement of Add Strings

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

415. Add Strings LeetCode Solution in C++

class Solution {
 public:
  string addStrings(string num1, string num2) {
    string ans;
    int carry = 0;
    int i = num1.length() - 1;
    int j = num2.length() - 1;

    while (i >= 0 || j >= 0 || carry) {
      if (i >= 0)
        carry += num1[i--] - '0';
      if (j >= 0)
        carry += num2[j--] - '0';
      ans += carry % 10 + '0';
      carry /= 10;
    }

    ranges::reverse(ans);
    return ans;
  }
};
/* code provided by PROGIEZ */

415. Add Strings LeetCode Solution in Java

class Solution {
  public String addStrings(String num1, String num2) {
    StringBuilder sb = new StringBuilder();
    int carry = 0;
    int i = num1.length() - 1;
    int j = num2.length() - 1;

    while (i >= 0 || j >= 0 || carry > 0) {
      if (i >= 0)
        carry += num1.charAt(i--) - '0';
      if (j >= 0)
        carry += num2.charAt(j--) - '0';
      sb.append(carry % 10);
      carry /= 10;
    }

    return sb.reverse().toString();
  }
}
// code provided by PROGIEZ

415. Add Strings LeetCode Solution in Python

class Solution:
  def addStrings(self, num1: str, num2: str) -> str:
    ans = []
    carry = 0
    i = len(num1) - 1
    j = len(num2) - 1

    while i >= 0 or j >= 0 or carry:
      if i >= 0:
        carry += int(num1[i])
      if j >= 0:
        carry += int(num2[j])
      ans.append(str(carry % 10))
      carry //= 10
      i -= 1
      j -= 1

    return ''.join(reversed(ans))
# code by PROGIEZ

Additional Resources

See also  342. Power of Four LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.