> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-27-2022-415.md).

# 01/27/2022 415

This problem is almost the same with the 67 Add Binary I did yesterday.

using two pointers i and j for length  of each string. set carry = 0.  When i or j >= 0, get the value for the index if the index >= 0, otherwise set the value = 0. Add them with carry. then carry = carry/2, sum = sum%2. append sum to the result. After the loop, if the carry != 0, append it to the result and reverse it.&#x20;

Time O(max(n,m)) n is the length of a, m is the length of b.

Space O(max(n,m))

class Solution { public String addStrings(String num1, String num2) {

```
    int i = num1.length() - 1;
    int j = num2.length() - 1;
    int carry = 0;
    StringBuilder sb = new StringBuilder();
    
    while(i >= 0 || j >=0){
        int x = (i >= 0) ? num1.charAt(i) - '0': 0;
        int y = (j >= 0) ? num2.charAt(j) - '0': 0;
        int sum = x + y + carry;
        carry = sum / 10;
        sum = sum % 10;
        sb.append(sum);
        i--;
        j--;  
    }
    if(carry != 0) sb.append(1);
    
    return sb.reverse().toString();    
    
}
```

}
