> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Add Strings Python Solution with Tests

> Tested Python solution for LeetCode 415 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 415, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/add-strings/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 415   # by problem number
lcpy gen -s add_strings   # by problem name
```

## Problem

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.

### Examples

```
Input: num1 = "11", num2 = "123"
Output: "134"
```

```
Input: num1 = "456", num2 = "77"
Output: "533"
```

```
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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_strings/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_strings/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(max(n, m))
    # Space: O(max(n, m))
    def add_strings(self, num1: str, num2: str) -> str:
        i, j = len(num1) - 1, len(num2) - 1
        digits: list[str] = []
        carry = 0
        while i >= 0 or j >= 0 or carry:
            total = carry
            if i >= 0:
                total += ord(num1[i]) - ord("0")
                i -= 1
            if j >= 0:
                total += ord(num2[j]) - ord("0")
                j -= 1
            carry, digit = divmod(total, 10)
            digits.append(chr(ord("0") + digit))
        return "".join(reversed(digits))
```

## Complexity

| Time | Space |
| - | - |
| O(max(n, m)) | O(max(n, m)) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.