LeetCode – Add Binary (Java)

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();
}
Category >> Algorithms >> Interview >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Ankit Shah

    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();
    }

  • Abhishek Nandgaonkar

    Why not use stock methods?
    System.out.println(Integer.toBinaryString(Integer.parseInt(“11”,2) + Integer.parseInt(“1”,2)));

  • Matias SM

    A “cheating” solution:

    String addBinary(String a, String b) {
    return new BigInteger(a, 2).add(new BigInteger(b, 2)).toString(2);
    }

  • Renzo Nuccitelli