Simple to understand Java solution with O(max(A.length(), B.length()))


#1
public class Solution {
    public String addBinary(String A, String B) {
        int carry = 0;
        int aIndex = A.length() - 1;
        int bIndex = B.length() - 1;
        String result = "";
        while (aIndex >= 0 || bIndex >= 0) {
            int aInt = (aIndex < 0)? 0: Character.getNumericValue(A.charAt(aIndex));
            int bInt = (bIndex < 0)? 0: Character.getNumericValue(B.charAt(bIndex));
            int sum = aInt + bInt + carry;
            int r = sum % 2;
            carry = sum / 2;
            aIndex--;
            bIndex--;
            result = Integer.toString(r) + result;
        }
        if(carry == 1) {
            result = Integer.toString(carry) + result;   
        }
        return result;
    }
}