# Add Strings

## Problem

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

### Thought Process

* This is very similar to **Add Binary**

* Have to remember that we can have potentailly have carry so we need a variable for this

## Solution

```
class Solution:
    def addStrings(self, num1: str, num2: str) -> str:
        i = len(num1)-1
        j = len(num2)-1
        carry = 0
        res = ""
        
        while i>=0 or j >= 0:
            numSum = carry
            if i >= 0:
                numSum+=ord(num1[i]) - ord('0')
            if j >= 0:
                numSum+=ord(num2[j]) - ord('0')
                
            carry = 1 if numSum > 9 else 0
            
            res+=str(numSum%10)
            
            i-=1
            j-=1
            
    
        if carry != 0:
            res+=str(carry)
        return res[::-1]
```

## Key Points

* We have to remember there can potenially be a carry variable if the sum of two digits is greater than 9

## Time Complexity

* **Time:** $$O(n)$$&#x20;
* **Space:** $$O(1)$$&#x20;


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://joshualbarb.gitbook.io/leetcode-problems/strings/string-math/add-strings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
