class Solution {
public String addBinary(String a, String b) {
if (a == null || a.length() == 0) {
return b;
}
if (b == null || b.length() == 0) {
return a;
}
StringBuilder builder = new StringBuilder();
char[] arra = a.toCharArray();
char[] arrb = b.toCharArray();
int pa = arra.length - 1;
int pb = arrb.length - 1;
int sum = 0;
while ((pa >= 0 || pb >= 0)) {
if (pa >= 0) {
sum += Character.getNumericValue(arra[pa]);
}
if (pb >= 0) {
sum += Character.getNumericValue(arrb[pb]);
}
builder.append(sum % 2);
sum /= 2;
pa--;
pb--;
}
if (sum == 1) {
builder.append(1);
}
return builder.reverse().toString();
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.addBinary("1111", "1"));
System.out.println(s.addBinary("11", "11"));
}
}