# 67. Add Binary

[Link](https://leetcode.com/problems/add-binary/)

Given two binary strings, return their sum (also a binary string).

The input strings are both **non-empty** and contains only characters `1` or `0`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```

**Example 2:**

```
Input: a = "1010", b = "1011"
Output: "10101"
```

**Solution**

記住要從a, b的尾部開始做運算。

```
public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int indexA = a.length()-1;
        int indexB = b.length()-1;
        int numA = 0, numB = 0, carry = 0;
        while(indexA >= 0 || indexB >= 0){
            
            if(indexA >= 0){
                numA = a.charAt(indexA) - '0';
            }
            else
                numA = 0;
            if(indexB >= 0){
                numB = b.charAt(indexB) - '0';
            }
            else
                numB = 0;
            int result = numA+numB+carry;
            sb.append(result%2);
            carry = result/2;
            indexA--;
            indexB--;
        }
        if(carry != 0){
            sb.append(carry);
        }
        return sb.reverse().toString();
    }
```
