> 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-26-2022-67.md).

# 01/26/2022 67

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 addBinary(String a, String b) {
     StringBuilder sb = new StringBuilder();
        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry=0;
        while(i>=0 || j>=0) {  
            int x= (i>=0)?a.charAt(i)-'0':0;
            int y= (j>=0)?b.charAt(j)-'0':0;
            int sum=x+y+carry;
            carry = sum/2;
            sb.append(sum%2);
            i--;
            j--;
        }
        if(carry!=0)
            sb.append(carry);
        return sb.reverse().toString();
        
    }
}
```
