Given two binary strings, return their sum (also a binary string).
For example, a = “11”, b = “1”, the return is “100”.
Java Solution
Very simple, nothing special. Note how to convert a character to an int.
public String addBinary(String a, String b) { if(a==null || a.length()==0) return b; if(b==null || b.length()==0) return a; int pa = a.length()-1; int pb = b.length()-1; int flag = 0; StringBuilder sb = new StringBuilder(); while(pa >= 0 || pb >=0){ int va = 0; int vb = 0; if(pa >= 0){ va = a.charAt(pa)=='0'? 0 : 1; pa--; } if(pb >= 0){ vb = b.charAt(pb)=='0'? 0: 1; pb--; } int sum = va + vb + flag; if(sum >= 2){ sb.append(String.valueOf(sum-2)); flag = 1; }else{ flag = 0; sb.append(String.valueOf(sum)); } } if(flag == 1){ sb.append("1"); } String reversed = sb.reverse().toString(); return reversed; } |
UPDATE: we can simply the method above.
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 sum=0; if(i>=0 && a.charAt(i)=='1'){ sum++; } if(j>=0 && b.charAt(j)=='1'){ sum++; } sum += carry; if(sum>=2){ carry=1; }else{ carry=0; } sb.insert(0, (char) ((sum%2) + '0')); i--; j--; } if(carry==1) sb.insert(0, '1'); return sb.toString(); } |
Awesome post!! Up to the point and crisp…
Thanks for sharing,
https://plus.google.com/collection/AQ2omB
well written!!
StringBuilder sb = new StringBuilder();
if (a == null || a.length() == 0) {
return b;
}
if (b == null || b.length() == 0) {
return a;
}
int aIndex = a.length() - 1;
int bIndex = b.length() - 1;
boolean hasCarry = false;
Character aCh = null;
Character bCh = null;
while (aIndex >= 0 || bIndex >= 0) {
aCh = null;
bCh = null;
if (aIndex >= 0) {
aCh = a.charAt(aIndex);
}
if (bIndex >= 0) {
bCh = b.charAt(bIndex);
}
if (aCh != null || bCh != null) {
aCh = (aCh == null) ? '0' : aCh;
bCh = (bCh == null) ? '0' : bCh;
if (aCh == bCh) {
if (hasCarry) {
sb.append('1');
} else {
sb.append('0');
}
hasCarry = (aCh == '1');
} else {
if (hasCarry) {
sb.append('0');
} else {
sb.append('1');
}
}
}
aIndex--;
bIndex--;
}
if (hasCarry) {
sb.append('1');
}
return sb.reverse().toString();
I have a similar solution but much simple, basically sum can be either 0,1,2 or 3, based on that the result and the carry will carry over. store everything in string builder and reverse at the end.
private static String binaryAddition(String a, String b) {
int i = a.length() - 1;
int j = b.length() - 1;
StringBuilder result = new StringBuilder();
int carry = 0;
int sum = 0;
while (i >= 0 || j >= 0) {
sum = carry;
if (i >= 0) {
sum = sum + (a.charAt(i) - '0');
i--;
}
if (j >= 0) {
sum = sum + (b.charAt(j) - '0');
j--;
}
if (sum == 1) {
result.append("1");
carry = 0;
} else if (sum == 2) {
result.append("0");
carry = 1;
} else if (sum == 3) {
result.append("1");
carry = 1;
} else {
result.append("0");
carry = 0;
}
}
if (carry == 1) {
result.append("1");
}
return result.reverse().toString();
}
Why not use stock methods?
System.out.println(Integer.toBinaryString(Integer.parseInt(“11”,2) + Integer.parseInt(“1”,2)));
A “cheating” solution:
String addBinary(String a, String b) {
return new BigInteger(a, 2).add(new BigInteger(b, 2)).toString(2);
}
Python solution: https://github.com/renzon/code_interview_training/blob/master/bynary_sum.py