My easy java solution with comments in 4ms


  • 0
    D

    public class Solution {
    public String addBinary(String a, String b) {

        //Base case: if a is null, return b
        if(b.length()==0 || b==null)
            return a;
        
        //Base case : if b is null, return a
        if(a.length()==0 || a==null)
            return b;
            
        //variable to keep track of carry
        int carry=0;
        
        //Stringbuilder to store the result
        StringBuilder sb=new StringBuilder();
        
        //keep track of the end of the strings
        int i=a.length()-1;
        int j=b.length()-1;
        
        while(i>=0 && j>=0)
        {
            //Case 1: when both characters are 1
            if(a.charAt(i)=='1' && b.charAt(j)=='1')
            {
                if(carry==0)
                {
                    sb.append("0");
                    carry=1;
                }
                else
                {
                    sb.append("1");
                    carry=1;
                }
            }
            //Case 2: when both characters are 0
            else if(a.charAt(i)=='0' && b.charAt(j)=='0')
            {
                if(carry==0)
                {
                    sb.append("0");
                    carry=0;
                }
                else
                {
                    sb.append("1");
                    carry=0;
                }
                
            }
            
            //Case 3: when either character is 0 or 1
            else
            {
                if(carry==0)
                {
                    sb.append("1");
                    carry=0;
                }
                else
                {
                    sb.append("0");
                    carry=1;
                }
            }
            
            //decrement indices of i and j
            i--;
            j--;
        }
        
        //if String a is remaining , add it to the result
        if(j<0)
        {
            while(i>=0)
        {    
            if(a.charAt(i)=='1')
            {
                if(carry==1)
                {
                    sb.append("0");
                    carry=1;
                }
                else
                {
                    sb.append("1");
                    carry=0;
                }
            }
            else
            {
                if(carry==1)
                {
                    sb.append("1");
                    carry=0;
                }
                else
                {
                    sb.append("0");
                    carry=0;
                }
            }
            
            i--;
        }
        }
        
        //if String b is remaining, add it to the result
        
        if(i<0)
        {
            while(j>=0)
        {    
            if(b.charAt(j)=='1')
            {
                if(carry==1)
                {
                    sb.append("0");
                    carry=1;
                }
                else
                {
                    sb.append("1");
                    carry=0;
                }
            }
            
            else
            {
                if(carry==1)
                {
                    sb.append("1");
                    carry=0;
                }
                else
                {
                    sb.append("0");
                    carry=0;
                }
            }
            
            j--;
        }
        }
        
        //if carry is 1, add it to the result
        if(carry==1)
            sb.append("1");
            
        
        //return result
        StringBuilder result=new StringBuilder();
        
        for(int k=sb.toString().length()-1;k>=0;k--)
        {
            result.append(sb.toString().charAt(k));
        }
        
        return result.toString();
        
            
        
        
    }
    

    }


  • 0
    B

    that code at the end triggered me.

    I think you should be able to just return sb.reverse().toString()


  • 0
    J
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.