# My JAVA Solution easy to understand

#1

public class Solution {
public String addBinary(String A, String B) {
int a = A.length()-1;
int b = B.length()-1;
int carry = 0;
StringBuilder ans = new StringBuilder();
while (a>=0 && b>=0){
char ch1 = A.charAt(a–);
char ch2 = B.charAt(b–);

``````        if (ch1=='0' && ch2=='0'){
if (carry==1){
ans.append(1);
carry = 0;
}
else{
ans.append(0);
}
}
else if (ch1=='1' && ch2=='0'){
if (carry==1){
ans.append(0);
carry = 1;
}
else{
ans.append(1);
}
}
else if (ch1=='0' && ch2=='1'){
if (carry==1){
ans.append(0);
carry = 1;
}
else{
ans.append(1);
}
}
else{
if (carry==1){
ans.append(1);
carry = 1;
}
else{
ans.append(0);
carry = 1;
}
}
}
while (a>=0){
char ch = A.charAt(a--);
if (ch=='0' && carry==0){
ans.append(0);
}
else if (ch=='1' && carry==0){
ans.append(1);
}
else if (ch=='0' && carry==1){
ans.append(1);
carry = 0;
}
else{
ans.append(0);
carry = 1;
}
}
while (b>=0){
char ch = B.charAt(b--);
if (ch=='0' && carry==0){
ans.append(0);
}
else if (ch=='1' && carry==0){
ans.append(1);
}
else if (ch=='0' && carry==1){
ans.append(1);
carry = 0;
}
else{
ans.append(0);
carry = 1;
}
}
if (carry!=0)
ans.append(carry);
return ans.reverse().toString();
}
``````

}